简体   繁体   English

通过data.frame中的名称和列号调用变量

[英]call variables by name and column number in a data.frame

I have a data frame with columns I want to reorder. 我有一个要重新排序的列的数据框。 However, in different iterations of my script, the total number of columns may change. 但是,在脚本的不同迭代中,列的总数可能会更改。

>Fruit
Vendor A B C D E ... Apples Oranges
Otto   4 5 2 5 2 ... 3      4

Fruit2<-Fruit[c(32,33,2:5)]

So instead of manually adapting the code (the columns 32 and 33 change) I'd like to do the following: 因此,除了手动修改代码(第32列和第33列更改)之外,我想执行以下操作:

Fruit2<-Fruit[,c("Apples", "Oranges", 2:5)]

I tried a couple of syntaxes but could not get it to do what I want. 我尝试了几种语法,但无法使其满足我的要求。 I know, this is a simple syntax issue, but I could not find the solution yet. 我知道,这是一个简单的语法问题,但我找不到解决方案。 The idea is to mix the variable name with the vector to reference the columns when writing a new data frame. 这个想法是在写一个新的数据帧时,将变量名和向量混合在一起以引用列。 I don't want to spell out the whole vector in variable names because in reality it's 30 variables. 我不想用变量名拼出整个向量,因为实际上它是30个变量。

I'm not sure how your data is stored in R, so this is what I used: 我不确定您的数据如何存储在R中,所以这是我使用的方式:

Fruit <- data.frame( "X1" = c("A",4),"X2" = c("B",5),"X3" = c("C",2),"X4"= 
c("D",5),"X5"= c("E",2),"X6" = c("Apples",3),"X7"= 
c("Oranges",4),row.names = c("Vendor","Otto"),stringsAsFactors = FALSE)

           X1 X2 X3 X4 X5     X6      X7
    Vendor  A  B  C  D  E Apples Oranges
    Otto    4  5  2  5  2      3       4

Then use: 然后使用:

indexes <- which(Fruit[1,]%in%c("Apples","Oranges"))
Fruit2<- Fruit[,c(indexes,2:5)]

Fruit[1,] references the Vendor row, and "%in%" returns a logical vector to the function "which". Fruit [1,]引用供应商行,而“%in%”将逻辑向量返回给函数“哪个”。 Then "which" returns indexes. 然后“哪个”返回索引。

This gives: 这给出:

    > Fruit2
               X6      X7 X2 X3 X4 X5
    Vendor Apples Oranges  B  C  D  E
    Otto        3       4  5  2  5  2

Make sure your data are not being stored as factors, otherwise this will not work. 确保您的数据没有作为因素存储,否则将无法正常工作。 Or you could change the Vendor row to column names as per the comment above. 或者,您可以根据上面的注释将“供应商”行更改为列名。

The answer is, as I found out, use the dplyr package. 答案是,正如我发现的那样,使用dplyr软件包。 It is very powerful. 它非常强大。

The solution to the aforementioned problem would be: 解决上述问题的方法是:

Fruit2<-Fruit %>% select(Apples,Oranges,A:E)

This allows dynamic selection of columns and lists of columns even if the indexes of the columns change. 即使列的索引发生更改,这也允许动态选择列和列列表。

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

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