如何使用R中的read.excel()将列指定为.factor?
我试图从R中的如何使用R中的read.excel()将列指定为.factor?,r,dplyr,readxl,R,Dplyr,Readxl,我试图从R中的Tidyverse(ReadXl)包中使用read\u Excel()函数从Excel文件中读取数据帧。我希望将列读取为.factor,但是,read\u Excel()中的col\u types参数函数似乎不支持作为因子 我现在使用以下解决方法: 示例代码: library(tidyverse) library(readxl) df <– read_excel("name_of_excel_file.xlsx") df <- df %>% mut
Tidyverse
(ReadXl
)包中使用read\u Excel()
函数从Excel文件中读取数据帧。我希望将列读取为.factor
,但是,read\u Excel()中的col\u types
参数
函数似乎不支持作为因子
我现在使用以下解决方法:
示例代码:
library(tidyverse)
library(readxl)
df <– read_excel("name_of_excel_file.xlsx")
df <- df %>%
mutate(column = as.factor(column))
库(tidyverse)
图书馆(readxl)
df参考:
尝试:
库(xlsx)
这是最好的了。有什么原因不适合你吗?没有<代码>列类型
不会将“因子”作为数据类型。当然,如果你愿意的话,你也可以在
处进行变异,这稍微少一些冗余。@camille它确实工作得很好,但是,这是一个我需要定期执行的操作,所以我想知道它是否可以以某种方式进行优化。
library(xlsx)
df <- read.xlsx("name_of_excel_file.xlsx", sheetName = "Sheet1",
stringsAsFactors = TRUE)