[英]Loop through dataframe column names - R
I'm trying to loop through the columns names of a dataframe, and evaluate which class is each column.我正在尝试遍历数据框的列名,并评估每列是哪个类。
for (i in columns(df)){
class(df$i)
}
I have tried everything, except the right way..我已经尝试了一切,除了正确的方法..
PS: I'm trying to do in this way because after I have to put different conditions for each class. PS:我尝试这样做是因为之后我必须为每个班级设置不同的条件。
To answer the exact question and fix the code given, see the example below要回答确切的问题并修复给定的代码,请参见下面的示例
df <- iris # data
for (i in colnames(df)){
print(class(df[[i]]))
}
# [1] "numeric"
# [1] "numeric"
# [1] "numeric"
# [1] "numeric"
# [1] "factor"
colnames
to get the column names of df
.您需要使用colnames
来获取df
的列名。df[[i]]
if you want to know the class of that.如果你想知道它的类别,你可以使用df[[i]]
访问每一列。 df[i]
is of class data.frame
. df[i]
属于data.frame
类。The problem was to loop through the columns of a dataframe, and an additional question was asked about looping through some subset of the dataframe.问题是遍历数据帧的列,并询问了一个关于遍历数据帧的某些子集的附加问题。 I used the mtcars dataset because it has more columns of data than the iris dataset.我使用了 mtcars 数据集,因为它的数据列比 iris 数据集多。 This allowed for a richer example.这允许提供更丰富的示例。 To loop through some subset of columns, use a numerical value in a for loop rather than using the names of the columns.要遍历某些列子集,请在 for 循环中使用数值而不是使用列的名称。 If columns of interest are regularly spaced then make a vector with the columns of interest.如果感兴趣的列有规律地间隔,那么用感兴趣的列制作一个向量。 Examples follow:示例如下:
#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.