简体   繁体   English

如何将数据帧分配给 R 列表中的特定位置

[英]How to assign dataframes to specific locations in a list in R

I've created 15 individual dataframes via a for loop, each one representing scores for each of the survey items comprising 15 different scales/topic areas.我通过 for 循环创建了 15 个单独的数据框,每个数据框代表每个调查项目的分数,包括 15 个不同的尺度/主题领域。 They're named df1 through df15 based on the original order of my topic areas, but I'd like to put them into a single list based on a different sort order.根据我的主题区域的原始顺序,它们被命名为 df1 到 df15,但我想根据不同的排序顺序将它们放入一个列表中。 I have a separate dataframe, called topic.area, listing each of the 15 topic areas, their original order, and it is arranged in the new sort order:我有一个单独的 dataframe,名为 topic.area,列出了 15 个主题区域中的每一个,它们的原始顺序,并以新的排序顺序排列:

Topic.Area...... orig.order... sort.order Topic.Area...... orig.order...... sort.order
Leadership........3................1领导力............3......1
Engagement..... 1...............2订婚...... 1............ 2
Innovation.........2................3创新............2......3
etc... ETC...

I essentially want to do the following, but the code isn't working for me.我基本上想做以下事情,但代码对我不起作用。 I'm very new to R, so I appreciate any solutions that aren't too advanced so I understand the logic behind them.我对 R 很陌生,所以我很欣赏任何不太先进的解决方案,所以我理解它们背后的逻辑。

myList<- list()

for(m in 1:15) {
 myList[[m]]<- paste("df", topic.area$orig.order[m], sep = "") 
}

I think the issue is you can't use the paste function in an assignment command, but I don't know to work around that.我认为问题是您不能在分配命令中使用粘贴 function ,但我不知道如何解决这个问题。

Get all the dataframe in a list.获取列表中的所有 dataframe。 As you have mentioned this is already ordered by orig.order .正如您所提到的,这已经由orig.order订购。

list_df <- mget(paste0('df', 1:15))

You can arrange them based on sort.order directly by doing:您可以通过执行以下操作直接根据sort.order排列它们:

sorted_list_df <- list_df[topic.area$sort.order]

Here is a completely reproducible example based on Pokémon Stats data.这是一个基于Pokémon Stats数据的完全可重现的示例。 Instead of just writing the input files to a list() , I use assign() to simulate a set of data frames in the global environment.我不只是将输入文件写入list() ,而是使用assign()来模拟全局环境中的一组数据帧。

download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
              "pokemonData.zip",
              method="curl",mode="wb")
unzip("pokemonData.zip")

thePokemonFiles <- list.files("./pokemonData",
                              full.names=TRUE)
filenames <- substr(list.files("./pokemonData",full.names = FALSE),1,5) 
results <- lapply(1:length(thePokemonFiles),function(x) {
     # read data and assign to a name in global environment 
     data <- read.csv(thePokemonFiles[x],stringsAsFactor = FALSE)
     assign(filenames[x],data,globalenv())
     return(TRUE)
})

At this point we have 7 data frames named gen01 to gen07 in the global environment.至此,我们在全局环境中有 7 个名为gen01gen07的数据帧。

在此处输入图像描述

Next, we generate the desired sort order as a vector.接下来,我们生成所需的排序顺序作为向量。 We'll use this along with the names from the filenames vector to get the data and build the output list in the desired sort order of data frames.我们将使用它和filenames向量中的名称来获取数据并以所需的数据帧排序顺序构建 output 列表。

desiredSortOrder <- c(1,3,5,7,2,4,6)
sortedDataFrames <- lapply(desiredSortOrder,function(x){
     get(filenames[x])
})
# verify generation numbers by printing first row of each data frame 
lapply(sortedDataFrames,function(x){
     x[1,c(1,2,12)]
})

Since the Generation column in each data frame matches its associated file name, we can verify the sort order by printing the first row of each data frame.由于每个数据帧中的Generation列与其关联的文件名匹配,我们可以通过打印每个数据帧的第一行来验证排序顺序。

>lapply(sortedDataFrames,function(x){
+      x[1,c(1,2,12)]
+ })
[[1]]
  Number      Name Generation
1      1 Bulbasaur          1

[[2]]
  Number    Name Generation
1    252 Treecko          3

[[3]]
  Number    Name Generation
1    494 Victini          5

[[4]]
  Number   Name Generation
1    722 Rowlet          7

[[5]]
  Number      Name Generation
1    152 Chikorita          2

[[6]]
  Number    Name Generation
1    387 Turtwig          4

[[7]]
  Number    Name Generation
1    650 Chespin          6

> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM