将所有数据帧字符列转换为因子 [英] Convert all data frame character columns to factors

查看:104
本文介绍了将所有数据帧字符列转换为因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个(预先存在的)具有各种类型列的数据框架,最简单的方式是将其所有字符列转换为因素,而不影响其他类型的列?



这是一个例子 data.frame

  df<  -  data.frame(A = factor(LETTERS [1:5]),
B = 1:5,C = as.logical(c(1,1,0,0,1)),
D =字母[1:5],
E =贴(LETTERS [1:5],字母[1:5]),
stringsAsFactors = FALSE)
df
#ABCDE
#1 A 1 TRUE a A a
#2 B 2 TRUE b B b
#3 C 3 FALSE c C c
#4 D 4 FALSE d D d
#5 E 5 TRUE e E e
str(df)
#'data.frame':5 obs。 5个变量:
#$ A:因子w / 5级别A,B,C,D,..:1 2 3 4 5
#$ B:int 1 2 3 4 5
#$ C:logi TRUE TRUE FALSE FALSE TRUE
#$ D:chrabcd...
#$ E :chrA aB bC cD d...

我知道我可以做:

  df $ D<  -  as.factor(df $ D)
df $是否有一种自动化这个过程的方法???????????????多一点?

解决方案

  DF<  -  data.frame(x = letters [1 :5],y = 1:5,stringsAsFactors = FALSE)

str(DF)
#'data.frame':5 obs。的2个变量:
#$ x:chrabcd...
#$ y:int 1 2 3 4 5

as.data.frame 的(烦人的)默认是将所有字符列进入因子列。您可以在这里使用:

  DF<  -  as.data.frame(unclass(DF))
str (DF)
#'data.frame':5 obs。的2个变量:
#$ x:因子w / 5级别a,b,c,d,..:1 2 3 4 5
#$ y:int 1 2 3 4 5


Given a (pre-existing) data frame that has columns of various types, what is the simplest way to convert all its character columns to factors, without affecting any columns of other types?

Here's an example data.frame:

df <- data.frame(A = factor(LETTERS[1:5]),
                 B = 1:5, C = as.logical(c(1, 1, 0, 0, 1)),
                 D = letters[1:5],
                 E = paste(LETTERS[1:5], letters[1:5]),
                 stringsAsFactors = FALSE)
df
#   A B     C D   E
# 1 A 1  TRUE a A a
# 2 B 2  TRUE b B b
# 3 C 3 FALSE c C c
# 4 D 4 FALSE d D d
# 5 E 5  TRUE e E e
str(df)
# 'data.frame':  5 obs. of  5 variables:
#  $ A: Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5
#  $ B: int  1 2 3 4 5
#  $ C: logi  TRUE TRUE FALSE FALSE TRUE
#  $ D: chr  "a" "b" "c" "d" ...
#  $ E: chr  "A a" "B b" "C c" "D d" ...

I know I can do:

df$D <- as.factor(df$D)
df$E <- as.factor(df$E)

Is there a way to automate this process a bit more?

解决方案

DF <- data.frame(x=letters[1:5], y=1:5, stringsAsFactors=FALSE)

str(DF)
#'data.frame':  5 obs. of  2 variables:
# $ x: chr  "a" "b" "c" "d" ...
# $ y: int  1 2 3 4 5

The (annoying) default of as.data.frame is to turn all character columns into factor columns. You can use that here:

DF <- as.data.frame(unclass(DF))
str(DF)
#'data.frame':  5 obs. of  2 variables:
# $ x: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
# $ y: int  1 2 3 4 5

这篇关于将所有数据帧字符列转换为因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆