我在 Rstudio 作業,并且有一個類似于以下內容的資料框:
Favorite<-c("Apple","Lemon","Orange","Salat","Onion", "Apple","Strawberry","Celery","Blueberry","Sweetpotatoes","Strawberry",
"Oragne","Celery","Sweetpotatoes","Onion","Blueberry","Strawberry","Salad")
PersonID<-c(67,82,67,21,02,12,90,23,65,32,44,67,56,77,30,198,20,99)
all_Data<-data.frame(PersonID,Favorite)
> head(all_Data)
PersonID Favorite
1 67 Apple
2 82 Lemon
3 67 Orange
4 21 Salat
5 2 Onion
6 12 Apple
我想再添加 3 列,它們應該包含以下內容:
如果 all_Data$Favorite 中的一行是 Apple 或 Blueberry,則 all_Data$Country = Ireand, all_Data$Continent= Europe and all_Data$city=Belfast
如果 all_Data$Favorite 中的一行是 Strawberry,則 all_Data$Country = Holland, all_Data$Continent= Europe and all_Data$city=Emmen
如果 all_Data$Favorite 中的一行是 Lemon 或 Orange,則 all_Data$Country = France, all_Data$Continent= Europe and all_Data$city=Menton
如果 all_Data$Favorite 中的一行是沙拉或洋蔥,則 all_Data$Country = Sweeden, all_Data$Continent= Europe and all_Data$city=Malmoe
如果 all_Data$Favorite 中的一行是 Lemon 或 Orange,則 all_Data$Country = France, all_Data$Continent= Europe and all_Data$city=Menton
如果 all_Data$Favorite 中的一行是 Sweetpotatoes,則 all_Data$Country = USA、all_Data$Continent= America 和 all_Data$city=Verona
如果 all_Data$Favorite 中的一行是 Celery,則 all_Data$Country = Germany、all_Data$Continent= Europe 和 all_Data$city=Berlin
library(tidyverse)
all_Data |>
mutate(ctry_cont = case_when(
str_detect(Favorite, "Appl|Blueb") ~ "Ireland|Europe",
str_detect(Favorite, "Straw") ~ "Brazillian|South's of America",
str_detect(Favorite, "Lemon|Orang") ~ "France|Europe",
str_detect(Favorite, "Salad|Onion") ~ "Sweden|Europe",
str_detect(Favorite, "Sweetpot") ~ "United of state|America",
str_detect(Favorite, "Celery") ~ "Germany|Europe",
TRUE ~ "Other|Other"
)) |>
separate(ctry_cont, c("country", "continent"))
運行上面的代碼后,我得到以下警告和資料,我們看到 United of Kingdom 和 United of America 值的一半。我還添加了帶撇號的單詞,因為在我的原始資料中有帶撇號的單詞,但它也不可見:
PersonID Favorite country continent
1 67 Apple Ireland Europe
2 82 Lemon France Europe
3 67 Orange France Europe
4 21 Salat Other Other
5 2 Onion Sweden Europe
6 12 Apple Ireland Europe
7 90 Strawberry Brazillian South
8 23 Celery Germany Europe
9 65 Blueberry Ireland Europe
10 32 Sweetpotatoes United of
11 44 Strawberry Brazillian South
12 67 Oragne Other Other
13 56 Celery Germany Europe
14 77 Sweetpotatoes United of
15 30 Onion Sweden Europe
16 198 Blueberry Ireland Europe
17 20 Strawberry Brazillian South
18 99 Salad Sweden Europe
Warning message:
Expected 2 pieces. Additional pieces discarded in 5 rows [7, 10, 11, 14, 17].
我還嘗試在代碼的最后一步添加 sep=""。它給出了一個錯誤。
separate(ctry_cont, c("country", "continent"), sep="")
uj5u.com熱心網友回復:
你可以做這樣的事情......
Favorite <- c(
"Apple",
"Lemon",
"Orange",
"Salad",
"Onion",
"Apple",
"Strawberry",
"Celery",
"Blueberry",
"Sweetpotatoes",
"Strawberry",
"Orange",
"Celery",
"Sweetpotatoes",
"Onion",
"Blueberry",
"Strawberry",
"Salad"
)
PersonID <-
c(67, 82, 67, 21, 02, 12, 90, 23, 65, 32, 44, 67, 56, 77, 30, 198, 20, 99)
all_Data <- data.frame(PersonID, Favorite)
library(tidyverse)
all_Data |>
mutate(ctry_cont = case_when(
str_detect(Favorite, "Appl|Blueb") ~ "Ireland, Europe",
str_detect(Favorite, "Straw") ~ "Holland, Europe",
str_detect(Favorite, "Lemon|Orang") ~ "France, Europe",
str_detect(Favorite, "Salad|Onion") ~ "Sweden, Europe",
str_detect(Favorite, "Sweetpot") ~ "United States, North America",
str_detect(Favorite, "Celery") ~ "Germany, Europe",
TRUE ~ "Other, Other"
)) |>
separate(ctry_cont, c("country", "continent"), sep = ", ")
#> PersonID Favorite country continent
#> 1 67 Apple Ireland Europe
#> 2 82 Lemon France Europe
#> 3 67 Orange France Europe
#> 4 21 Salad Sweden Europe
#> 5 2 Onion Sweden Europe
#> 6 12 Apple Ireland Europe
#> 7 90 Strawberry Holland Europe
#> 8 23 Celery Germany Europe
#> 9 65 Blueberry Ireland Europe
#> 10 32 Sweetpotatoes United States North America
#> 11 44 Strawberry Holland Europe
#> 12 67 Orange France Europe
#> 13 56 Celery Germany Europe
#> 14 77 Sweetpotatoes United States North America
#> 15 30 Onion Sweden Europe
#> 16 198 Blueberry Ireland Europe
#> 17 20 Strawberry Holland Europe
#> 18 99 Salad Sweden Europe
由reprex 包于 2022-04-22 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/461630.html
上一篇:使用Jquery,如何使用if/else條件更改顏色
下一篇:從我單擊的按鈕元素中獲取值
