【问题标题】:using for loop to extract vectors from multiple data frames使用 for 循环从多个数据帧中提取向量
【发布时间】:2021-02-26 10:55:51
【问题描述】:

我有 50 个数据框,每个数据框的结构都相同(每个都有六个变量,不超过 300 行)。我需要从每个数据帧中提取和转换两个向量,它们对应于一些但不是所有的行。

所以我从每个数据集中提取两列,然后从这些列中提取几行。

所有 50 个向量都绑定到一个矩阵中,随后用于网络分析(一个引文矩阵,就其价值而言——因此是一个有向图)。

下面的代码得到了这个提取和转换。

library(tidyverse)

# read the original .csv file and extract the relevant
# vectors

SOME_JOURNAL <- read_csv("SOME_JOURNAL.csv") %>%
 select(X3, X4) %>% 
 rename("journal" = X3,
        "citations" = X4) %>%
 mutate(citations = as.integer(citations)) %>% 
 na.omit() %>% 
 tail(-3)

# identify the specfiic rows I want to extract
extract_list <- sort(c("SOME_JOURNAL",
                   "ANOTHER_JOURNAL",
                  "YET_ANOTHER_JOURNAL",
                  "ONE_MORE_JOURNAL"))

# extract the rows
SOME_JOURNAL <- SOME_JOURNAL %>% 
 filter(!!sym(names(.)[1]) %in% extract_list) %>% 
 # filters out the items I want  
 add_row(journal = setdiff(extract_list, SOME_JOURNALL$journal), citations = 0) %>% 
 # adds rows for which there is no data and assigns them zeros
 arrange(journal) %>%
 # need things in alphabetical order to manage things later on
 pivot_wider(names_from = journal, values_from = citations) 
# transposes the vector so that I can bind it with other vectors as a matrix
# for a directed graph

# make another adjustment to help transforming the matrix into a graph
SOME_JOURNAL <- data.frame(SOME_JOURNAL, row.names = "SOME_JOURNAL")

# create thee matrix by binding extracted vectors
matrix <- as.matrix(rbind(SOME_JOURNAL,
                          ANOTHER_JOURNAL,
                          YET_ANOTHER_JOURNAL,
                          ONE_MORE_JOURNAL))

reprex package (v0.3.0) 于 2021-02-26 创建

鉴于我有 50 个这样的数据框,我想自动执行此操作。而且我遇到了障碍(主要是因为我是新手)。下面的代码导致“$ 运算符对原子向量无效”错误。我尝试使用 [ 和 [[ 但我不知道这是否是可能有帮助的解决方案。

任何帮助将不胜感激。

library(tidyverse)

# get a list of all the filenames
filenames <- list.files(path="data/",
                        pattern=".*csv")

# for loop to read files and extract vectors 
for(i in filenames){
 filepath <- file.path("data/", paste(i))
 short_name <-str_replace_all(str_remove_all(i,
                                             "#.*"), "-", "_")
 # the data frames have very long names; this just shortens them
 assign(short_name, read_csv(filepath) %>% 
         select(X3, X4) %>% 
         rename("journal" = X3,
                "citations" = X4) %>%
         mutate(citations = as.integer(citations)) %>% 
         na.omit() %>% 
         tail(-2) %>%
         filter(!!sym(names(.)[1]) %in% extract_list) %>% 
         # everything works fine to this point; the code after produces 
         # the "$ operator is invalid for atomic vectors" error
         add_row(journal = setdiff(extract_list, SOME_JOURNAL$journal), 
                 citations = 0) 
        
 )  
}

reprex package (v0.3.0) 于 2021 年 2 月 26 日创建

【问题讨论】:

  • 您的第二个代码提取中有SOME_JOURNALL,第一个代码提取中有SOME_JOURNAL
  • @Limey 谢谢。那是reprex的错字;现在已更正。它是/不对错误负责。
  • 我无法用虚构的数据重现错误。没有看到您的输入,我无法进一步调查。我怀疑这是您输入数据的意外功能。如果您可以确定循环的哪个迭代导致错误,那可能会有所帮助。另外,请参阅this post 以获取有关创建良好 MWE 的建议:帮助我们帮助您..

标签: r tidyverse


【解决方案1】:

我不能把这个解决方案归功于我,但解决它的人对我来说很好,我将它发布给后代。

引用求解器的话,“总体思路是将长管道拆分为多个步骤,以便 setdiff() 步骤可以访问数据帧中的日志向量”。

可能还有其他方法可以做到这一点,但这符合我的目的。

for(i in filenames){
        filepath <- file.path("data/", paste(i))
        short_name <-str_replace_all(str_remove_all(i,
                                                    "#.*"), "-", "_")
        # the data frames have very long names; this just shortens them
        df <- read_csv(filepath) %>% 
                select(X3, X4) %>% 
                rename("journal" = X3,
                       "citations" = X4) %>%
                mutate(citations = as.integer(citations)) %>% 
                na.omit() %>% 
                tail(-2) %>%
                filter(!!sym(names(.)[1]) %in% extract_list)
        df_all_rows <- df %>%
                add_row(journal = setdiff(extract_list, df$journal), 
                        citations = 0) %>% 
                arrange(journal) %>% 
                pivot_wider(names_from = journal, values_from = citations)
        df_all_rows <- data.frame(df_all_rows, row.names = short_name)
        assign(short_name, df_all_rows)
}
#> Error in eval(expr, envir, enclos): object 'filenames' not found

reprex package (v0.3.0) 于 2021-02-26 创建

【讨论】:

    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2014-08-05
    • 2023-03-19
    • 2019-11-28
    • 2019-11-23
    相关资源
    最近更新 更多