循环遍历数据框列名 - R

时间:2018-04-18 00:36:31

标签: r loops dataframe

我正在尝试遍历数据框的列名,并评估每列的哪个类。

for (i in columns(df)){
  class(df$i)
}

除了正确的方法,我已经尝试了一切......

PS:我试图以这种方式做,因为我必须为每个班级设置不同的条件。

2 个答案:

答案 0 :(得分:8)

要回答确切的问题并修复给定的代码,请参阅下面的示例

df <- iris # data

for (i in colnames(df)){
   print(class(df[[i]]))
}
# [1] "numeric"
# [1] "numeric"
# [1] "numeric"
# [1] "numeric"
# [1] "factor"
  1. 您需要使用colnames来获取df的列名。
  2. 如果您想了解其中的类,则可以使用df[[i]]访问每列。 df[i]属于data.frame类。

答案 1 :(得分:0)

问题是要遍历数据框的各列,还提出了有关遍历数据框的某些子集的另一个问题。我使用了mtcars数据集,因为它具有比虹膜数据集更多的数据列。这提供了更丰富的示例。要遍历某些列子集,请在for循环中使用数字值,而不要使用列名。如果感兴趣的列是规则间隔的,则用感兴趣的列作一个向量。示例如下:

#Similar to previous answer only with mtcars rather than iris data.
df2<-mtcars
for (i in colnames(df2)){print(paste(i,"  ",class(df2[[i]])))}

#An alternative that is as simple but does not also print the variable names.
df2<-mtcars
for (i in 1:ncol(df2)){print(paste(i,"  ",class(df2[[i]])))}

#With variable names:
df2<-mtcars
for (i in 1:ncol(df2)){print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}

#Now that we are looping numerically one can start in column 3 by:
df2<-mtcars
for (i in 3:ncol(df2)){print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}

#To stop before the last column add a break statement inside an if
df2<-mtcars
for (i in 3:ncol(df2)){
  if(i>7){break}
  print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}

#Finally, if you know the columns and they are irregularly spaced try this:
UseCols<-c(2,4,7,9,10)
for (i in UseCols){print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}