Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

loops - Convert the last column of several dataframes to a factor in R

I have three dataframes in R, df1, df2, df3 that contain different data, however, the last column contains my target variable for each dataset. However, in each instance, the last column is an integer and I want it to be a factor. I know I can do the following code to convert the column into a factor:

df1[,'lascoldf1'] <-factor(df1[,'lastcoldf1'])
df2[,'lascoldf2'] <-factor(df2[,'lastcoldf2'])
df3[,'lascoldf3'] <-factor(df3[,'lastcoldf3'])

However, this seems a little inefficient. Also, the last colname isn't always the same name, and each dataset has a different number of columns, so I just need a way of referencing the last column. I've found this way so would probably need something like the following pseudo-code:

for (df in c(df1,df2,df3)){
index = df[,ncol(df)]
lapply(df[,index] , factor)
}

However, I can't seem to get it to work properly.

question from:https://stackoverflow.com/questions/65841901/convert-the-last-column-of-several-dataframes-to-a-factor-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Put the data frames into a list and in an lapply use ncol to identify the last column.

L <- lapply(list(df1, df2, df3), function(x) {x[,ncol(x)] <- as.factor(x[,ncol(x)]);x})
str(L[[1]])
# 'data.frame': 3 obs. of  4 variables:
#  $ X1: int  1 2 3
#  $ X2: int  4 5 6
#  $ X3: int  7 8 9
#  $ X4: Factor w/ 3 levels "10","11","12": 1 2 3

Data:

df1 <- df2 <- df3 <- data.frame(matrix(1:12, 3, 4))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...