如何替换多列中的多个字符串响应
How to replace multiple string responses in multiple columns
我导入了一个包含许多列和许多响应的 csv 数据集。我想查看特定的列并替换一组响应。
在我的数据集中,我有:hairtypeDad、hairtypeMom、hairtypeBro1、hairtypeSis1,它们都位于我文件的不同区域。其中有很多我想改的回复,包括但不限于:
Straight= straightened,
Curly= curled
Wavy = waved
wavyy=waved
cruley= curled
等等。
下面是我目前试过的代码:
hairdata <- read.csv ('alldata.csv', header = TRUE, stringAsFactors = FALSE)
hair_vars<- c ("hairtypeDad", "hairtypeMom", "hairtypeBro1", "hairtypeSis1")
hairdata[hair_vars]<-str_replace_all(hairdata[hair_vars],
c("Straight"= "straightened",
"Curly"= "curled",
"Wavy" = "waved",
"wavyy"= "waved"))
#I also tried:
hairdata %>% mutate(across(c("hairtypeDad", "hairtypeMom", "hairtypeBro1", "hairtypeSis1"),
fns= ~ str_replace_all(.,
c("Straight"= "straightened",
"Curly"= "curled",
"Wavy" = "waved",
"wavyy"= "waved"))
最终,我希望它来自:
id
hairtypeMom
hairtypeDad
hairtypeBro1
1
Straight
Curly
wavyy
2
Wavy
Curly
Curly
到
id
hairtypeMom
hairtypeDad
hairtypeBro1
1
straightened
curled
waved
2
waved
curled
curled
我没有得到我需要的东西。请帮忙!!
你们非常亲密;您只是错过了 .fns =
中的句点,因为您错过了 fns =
。您还遗漏了几个右括号。
library(tidyverse)
df %>%
mutate(across(
c("hairtypeDad", "hairtypeMom", "hairtypeBro1"),
.fns = ~ str_replace_all(.,
c("Straight" = "straightened",
"Curly" = "curled",
"Wavy" = "waved",
"wavyy" = "waved")
)
))
输出
id hairtypeMom hairtypeDad hairtypeBro1
1 1 straightened curled waved
2 2 waved curled curled
数据
df <- structure(list(id = 1:2, hairtypeMom = c("Straight", "Wavy"),
hairtypeDad = c("Curly", "Curly"), hairtypeBro1 = c("wavyy",
"Curly")), class = "data.frame", row.names = c(NA, -2L))
我导入了一个包含许多列和许多响应的 csv 数据集。我想查看特定的列并替换一组响应。
在我的数据集中,我有:hairtypeDad、hairtypeMom、hairtypeBro1、hairtypeSis1,它们都位于我文件的不同区域。其中有很多我想改的回复,包括但不限于:
Straight= straightened,
Curly= curled
Wavy = waved
wavyy=waved
cruley= curled
等等。
下面是我目前试过的代码:
hairdata <- read.csv ('alldata.csv', header = TRUE, stringAsFactors = FALSE)
hair_vars<- c ("hairtypeDad", "hairtypeMom", "hairtypeBro1", "hairtypeSis1")
hairdata[hair_vars]<-str_replace_all(hairdata[hair_vars],
c("Straight"= "straightened",
"Curly"= "curled",
"Wavy" = "waved",
"wavyy"= "waved"))
#I also tried:
hairdata %>% mutate(across(c("hairtypeDad", "hairtypeMom", "hairtypeBro1", "hairtypeSis1"),
fns= ~ str_replace_all(.,
c("Straight"= "straightened",
"Curly"= "curled",
"Wavy" = "waved",
"wavyy"= "waved"))
最终,我希望它来自:
id | hairtypeMom | hairtypeDad | hairtypeBro1 |
---|---|---|---|
1 | Straight | Curly | wavyy |
2 | Wavy | Curly | Curly |
到
id | hairtypeMom | hairtypeDad | hairtypeBro1 |
---|---|---|---|
1 | straightened | curled | waved |
2 | waved | curled | curled |
我没有得到我需要的东西。请帮忙!!
你们非常亲密;您只是错过了 .fns =
中的句点,因为您错过了 fns =
。您还遗漏了几个右括号。
library(tidyverse)
df %>%
mutate(across(
c("hairtypeDad", "hairtypeMom", "hairtypeBro1"),
.fns = ~ str_replace_all(.,
c("Straight" = "straightened",
"Curly" = "curled",
"Wavy" = "waved",
"wavyy" = "waved")
)
))
输出
id hairtypeMom hairtypeDad hairtypeBro1
1 1 straightened curled waved
2 2 waved curled curled
数据
df <- structure(list(id = 1:2, hairtypeMom = c("Straight", "Wavy"),
hairtypeDad = c("Curly", "Curly"), hairtypeBro1 = c("wavyy",
"Curly")), class = "data.frame", row.names = c(NA, -2L))