从这个数据帧:
df <- data.frame(id = c(1,2,3,4),
scores = c(-1.1,0.2,1.3,-0.4),
col2 = c(1,0,1,0),
col2 = c(1,1,1,1), col3 = c(-0.3,-0.2,3.3,-2.4))
如果我们使用 str(df(,我们可以看到列是 int 或 num。
如何转换为因子。
示例类似于此数据帧的 str(df(:
df <- data.frame(id = c('1','2','3','4'),
scores = c('-1.1','0.2','1.3','-0.4'),
col2 = c('1','0','1','0'),
col2 = c('1','1','1','1'), col3 = c('-0.3','-0.2','3.3','-2.4'))
有了tidyverse
,我们就可以使用mutate_all
library(dplyr)
df %>%
mutate_all(factor)