问题将色谱柱从角色转换为因素,使用突变中
当我运行以下代码以尝试将几列数据从字符转换为因素时,我正在遇到一个问题:
library(readxl)
library(dplyr)
"LR_df1"<-read_excel("Correct file location.xlsx",sheet="Correct sheet")
LR_df1 %>%
as_tibble()%>%
mutate(across(c(SiteCode, S01UsualResidence), as.factor))
初始输出看起来不错,看来指定的列已转换为:
# A tibble: 1,075 x 238
A CaseId SiteCode S01Gender S01UsualResidence
<dbl> <dbl> <fct> <chr> <fct>
但是当我运行时:
summary(LR_df1$SiteCode)
我得到:
&gt;摘要(lr_df1 $ sitecode)长度类模式1075字符
,
因此似乎尚未转换该变量。我会非常感谢可以向我展示我做错什么的人。
I am encountering a problem when I run the following code to try to convert several columns of data from character to factor:
library(readxl)
library(dplyr)
"LR_df1"<-read_excel("Correct file location.xlsx",sheet="Correct sheet")
LR_df1 %>%
as_tibble()%>%
mutate(across(c(SiteCode, S01UsualResidence), as.factor))
The initial output looks good, and it appears that the specified columns have been converted:
# A tibble: 1,075 x 238
A CaseId SiteCode S01Gender S01UsualResidence
<dbl> <dbl> <fct> <chr> <fct>
But then when I run:
summary(LR_df1$SiteCode)
I get:
> summary(LR_df1$SiteCode) Length Class Mode 1075 character character
So it appears that the variable hasn't been converted. I'd be very grateful to anyone who can show me what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mutate()
与dplyr
的所有其他功能一样,不会永久更改其工作的数据框架。它本身将输出更改的数据框架,但原始数据框架保持完整。如果您希望它永久更改数据框架,则需要重新分配:
mutate()
, like all other functions fromdplyr
, doesn't permanently change the dataframe it works on. On its own, it will output a changed dataframe, but the original dataframe stays intact.If you want it to permanently change the dataframe, you need to reassign: