从 int 和 num 转换为因子

  • 本文关键字:转换 num int r
  • 更新时间 :
  • 英文 :


从这个数据帧:

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)

最新更新