繁体   English   中英

循环遍历数据框列名 - R

[英]Loop through dataframe column names - R

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

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

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

PS:我尝试这样做是因为之后我必须为每个班级设置不同的条件。

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

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类。

问题是遍历数据帧的列,并询问了一个关于遍历数据帧的某些子集的附加问题。 我使用了 mtcars 数据集,因为它的数据列比 iris 数据集多。 这允许提供更丰富的示例。 要遍历某些列子集,请在 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]])))}

暂无
暂无

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

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