表列表和数据框变量列表

时间:2019-04-11 13:36:47

标签: r list dataframe

我可能有成千上万张表,我想从每个表中提取特定变量,转换成一个单独的数据框,用“ df_prefix”命名,然后进行分析。起始表为:

activities
  line_number    id       name  startdate
1       21000 ab123   John Doe 2010-11-01
2       23400 cd456 Peter Gynn 2008-03-25
3       26800 ef789 Jolie Hope 2007-03-14

和:

 classifications
  id var1 var2
1  1    a    1
2  2    b    1
3  3    c    0
4  4    d    0
5  5    e    1

我想要的活动表和分类表的输出是:

activities[,c("id", "line_number", "name")]
     id line_number       name
1 ab123       21000   John Doe
2 cd456       23400 Peter Gynn
3 ef789       26800 Jolie Hope

和分类表:

  classifications[, c("id")]
1                          1
2                          2
3                          3
4                          4
5                          5

我已经看过使用list2df和list2env。我不太确定该怎么办。.tables[[[1]] [,fields [[1]]]绝对行不通..

#Make a fake activities table
line_number <- c(21000, 23400, 26800)
id <- c('ab123','cd456','ef789')
name <- c('John Doe','Peter Gynn','Jolie Hope')
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
activities <- data.frame(line_number,id,name, startdate, stringsAsFactors=FALSE)

#Make a fake classifications table
classifications <- data.frame(id = c(1, 2, 3, 4, 5),
                  var1 = c('a', 'b', 'c', 'd', 'e'),
                  var2 = c(1, 1, 0, 0, 1))

#Make list of tables and list of specific fields we need
fields <- list(a = c("id", "line_number", "name"), c = "id")
tables<-list("activities","classifications")

1 个答案:

答案 0 :(得分:0)

这可行,但我想知道是否有一种方法可以自动命名表,而不是复制数据帧的原始列表,即table_names。理想情况下,我想给它一个数据框列表,它会自动为它们命名。

#Make a fake activities table
line_number <- c(21000, 23400, 26800)
id <- c('ab123','cd456','ef789')
name <- c('John Doe','Peter Gynn','Jolie Hope')
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
activities <- data.frame(line_number,id,name, startdate, stringsAsFactors=FALSE)

#Make a fake classifications table
classifications <- data.frame(id = c(1, 2, 3, 4, 5),
                  var1 = c('a', 'b', 'c', 'd', 'e'),
                  var2 = c(1, 1, 0, 0, 1))

#Make list of tables and list of specific fields we need
fields <- list(a = c("id", "line_number", "name"), c = "id")
tables<-list(activities,classifications)
#table_names<-list("activities_df", "classifications_df") #This is clunky
table_names<-names(Filter(isTRUE, eapply(.GlobalEnv, is.data.frame)))

for(i in 1:length(tables)) {
x=tables[[i]]
y=fields[[i]]
z<-as.data.frame(x[,y])
name <- paste(table_names[i],sep="") 
assign(name,z) 
}