[英]R -apply- convert many columns from numeric to factor
I need to convert many columns that are numeric to factor type.我需要将许多数字列转换为因子类型。 An example table:
示例表:
df <- data.frame(A=1:10, B=2:11, C=3:12)
I tried with apply:我试过申请:
cols<-c('A', 'B')
df[,cols]<-apply(df[,cols], 2, function(x){ as.factor(x)});
But the result is a character class.但结果是一个字符类。
> class(df$A)
[1] "character"
How can I do this without doing as.factor for each column?如何在不为每一列做 as.factor 的情况下做到这一点?
Try尝试
df[,cols] <- lapply(df[,cols],as.factor)
The problem is that apply()
tries to bind the results into a matrix, which results in coercing the columns to character:问题是
apply()
试图将结果绑定到一个矩阵中,这导致将列强制为字符:
class(apply(df[,cols], 2, as.factor)) ## matrix
class(as.factor(df[,1])) ## factor
In contrast, lapply()
operates on elements of lists.相比之下,
lapply()
对列表的元素进行操作。
Updated Nov 9, 2017 2017 年 11 月 9 日更新
purrr / purrrlyr are still in development purrr / purrrlyr 仍在开发中
Similar to Ben's, but using purrrlyr::dmap_at
:类似于 Ben 的,但使用
purrrlyr::dmap_at
:
library(purrrlyr)
df <- data.frame(A=1:10, B=2:11, C=3:12)
# selected cols to factor
cols <- c('A', 'B')
(dmap_at(df, factor, .at = cols))
A B C
<fctr> <fctr> <int>
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
8 9 10
9 10 11
10 11 12
您可以将您的结果放回一个数据框中,以识别这些因素:
df[,cols]<-data.frame(apply(df[,cols], 2, function(x){ as.factor(x)}))
Another option, with purrr
and dplyr
, perhaps a little more readable than the base solutions, and keeps the data in a dataframe:另一种选择,使用
purrr
和dplyr
,可能比基本解决方案更具可读性,并将数据保存在数据帧中:
Here's the data:这是数据:
df <- data.frame(A=1:10, B=2:11, C=3:12)
str(df)
'data.frame': 10 obs. of 3 variables:
$ A: int 1 2 3 4 5 6 7 8 9 10
$ B: int 2 3 4 5 6 7 8 9 10 11
$ C: int 3 4 5 6 7 8 9 10 11 12
We can easily operate on all columns with dmap
:我们可以使用
dmap
轻松地对所有列进行dmap
:
library(purrr)
library(dplyr)
# all cols to factor
dmap(df, as.factor)
Source: local data frame [10 x 3]
A B C
(fctr) (fctr) (fctr)
1 1 2 3
2 2 3 4
3 3 4 5
4 4 5 6
5 5 6 7
6 6 7 8
7 7 8 9
8 8 9 10
9 9 10 11
10 10 11 12
And similarly use dmap
on a subset of columns using select
from dplyr
:而同样使用
dmap
使用列的子集select
从dplyr
:
# selected cols to factor
cols <- c('A', 'B')
df[,cols] <-
df %>%
select(one_of(cols)) %>%
dmap(as.factor)
To get the desired result:要获得所需的结果:
str(df)
'data.frame': 10 obs. of 3 variables:
$ A: Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10
$ B: Factor w/ 10 levels "2","3","4","5",..: 1 2 3 4 5 6 7 8 9 10
$ C: int 3 4 5 6 7 8 9 10 11 12
A simple but effective option would be mapply
一个简单但有效的选择是
mapply
df <- data.frame(A=1:10, B=2:11, C=3:12)
cols <- c('A', 'B')
df[,cols] <- as.data.frame(mapply(as.factor,df[,cols]))
You can also use for-loop to achieve the same result:您还可以使用 for 循环来实现相同的结果:
for(col in cols){
df[,col] <- as.factor(df[,col])
}
Here are couple of tidyverse
options -这里有几个
tidyverse
选项 -
library(dplyr)
cols <- c('A', 'B')
df <- df %>% mutate(across(all_of(cols), factor))
str(df)
#'data.frame': 10 obs. of 3 variables:
# $ A: Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10
# $ B: Factor w/ 10 levels "2","3","4","5",..: 1 2 3 4 5 6 7 8 9 10
# $ C: int 3 4 5 6 7 8 9 10 11 12
Using map
-使用
map
-
df[cols] <- purrr::map(df[cols], factor)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.