r - 突变改变所有值
问题描述
我正在尝试使用以下代码执行突变:
set <- c('1 star', '2 stars', '3 stars', '4 stars', '5 stars')
set <- as.data.frame(set)
names(set) <- c('score')
set$score <- as.factor(set$score)
set <- set %>% mutate(score = ifelse(score == '1 star', 1, score))
我期望代码将包含值的单元格更改1 star
为1
,如果值不是1 star
,我希望它保持原样。但是,它似乎更改了所有行中的值,包括那些具有其他值的单元格1 star
。
我的推理出了什么问题?
解决方案
替换时将值更改为字符。
library(dplyr)
set <- set %>% mutate(score = ifelse(score == '1 star', 1, as.character(score)))
# score
#1 1
#2 2 stars
#3 3 stars
#4 4 stars
#5 5 stars
推荐阅读
- angular - 开玩笑地模拟 window.open
- ruby - 红宝石:未定义的方法排除?
- python - 如何在python中查找数字中的小数位数也计算尾随零
- vue.js - 如何继承Vue Json
- django - 为灯具转换 csv
- digital-ocean - 重定向错误的 URL/路径 DigitalOcean Spaces
- git - 带有 GitFlow 分支的 Azure DevOps 构建和发布管道
- java - metaData.getPrimaryKeys() 在 key 为单个时返回四个相同的记录
- angular - 在 URL 中使用变量时出现 404 错误:模型 [App\Models\Seeker] 没有查询结果
- dll - 从命令行运行 DLL 文件以弹出消息框