R 具有多列的嵌套ifelse语句
我有一个数据集,其中包括(除其他许多变量外)5列,表示数据来自的国家,编码为数字。我想创建一个新变量,用纯文本表示国家(例如西班牙而不是312) 以下是一个数据样本,仅5行2列用于再现性:R 具有多列的嵌套ifelse语句,r,if-statement,dplyr,mutate,R,If Statement,Dplyr,Mutate,我有一个数据集,其中包括(除其他许多变量外)5列,表示数据来自的国家,编码为数字。我想创建一个新变量,用纯文本表示国家(例如西班牙而不是312) 以下是一个数据样本,仅5行2列用于再现性: c <- structure(list(CountryAP = structure(c(109, NA, 124, NA, NA), label = "Country of the Child Helpline (Asia Pacific region)", labels = c(Afghanistan
c <- structure(list(CountryAP = structure(c(109, NA, 124, NA, NA), label = "Country of the Child Helpline (Asia Pacific region)", labels = c(Afghanistan = 109, `New Zealand` = 124), class = "haven_labelled"),
CountryEr = structure(c(NA, 313, NA, 287, 278), label = "Country of the Child Helpline (Europe region)", labels = c( Azerbaijan = 278, Finland = 287, Sweden = 313), class = "haven_labelled")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L))
当我只运行CountryEr部分时,它会正确运行
知道如何让ifelse语句接受另一个变量吗
任何帮助都将不胜感激 谢谢你,case_确实解决了我的问题:
c <- c %>% mutate(Country = case_when(CountryAP == 109 ~ 'Afghanistan',
CountryAP == 124 ~ 'New Zealand',
CountryEr == 313 ~ 'Sweden',
CountryEr == 287 ~ 'Finland',
CountryEr == 278 ~ 'Azerbaijan'))
c%突变(Country=case_当(CountryAP=109~‘阿富汗’,
CountryAP==124~‘新西兰’,
CountryEr==313~‘瑞典’,
CountryEr==287~‘芬兰’,
CountryEr==278~‘阿塞拜疆’)
谢谢您,case\u在解决我的问题时:
c <- c %>% mutate(Country = case_when(CountryAP == 109 ~ 'Afghanistan',
CountryAP == 124 ~ 'New Zealand',
CountryEr == 313 ~ 'Sweden',
CountryEr == 287 ~ 'Finland',
CountryEr == 278 ~ 'Azerbaijan'))
c%突变(Country=case_当(CountryAP=109~‘阿富汗’,
CountryAP==124~‘新西兰’,
CountryEr==313~‘瑞典’,
CountryEr==287~‘芬兰’,
CountryEr==278~‘阿塞拜疆’)
我可以想出两种方法来做到这一点。首先,您需要将国家代码统一到一列中:
c <- c %>%
mutate(CountryCode = ifelse(is.na(CountryAP), CountryEr, CountryAP))
CountryAP CountryEr CountryCode
<dbl> <dbl> <dbl>
1 109 NA 109
2 NA 313 313
3 124 NA 124
4 NA 287 287
5 NA 278 278
合并辅助表
或者,您可以将国家代码和国家名称值存储在单独的表中,并将它们合并到主数据中:
df.countries <- data.frame(
CountryCode = c(109, 124, 313, 287, 278),
CountryName = c('Afghanistan', 'New Zealand', 'Sweden', 'Finland', 'Azerbaijan')
)
CountryCode CountryName
1 109 Afghanistan
2 124 New Zealand
3 313 Sweden
4 287 Finland
5 278 Azerbaijan
c <- c %>%
left_join(df.countries, by = 'CountryCode')
CountryAP CountryEr CountryCode CountryName
<dbl> <dbl> <dbl> <chr>
1 109 NA 109 Afghanistan
2 NA 313 313 Sweden
3 124 NA 124 New Zealand
4 NA 287 287 Finland
5 NA 278 278 Azerbaijan
df.countries我可以想出两种方法。首先,您需要将国家代码统一到一列中:
c <- c %>%
mutate(CountryCode = ifelse(is.na(CountryAP), CountryEr, CountryAP))
CountryAP CountryEr CountryCode
<dbl> <dbl> <dbl>
1 109 NA 109
2 NA 313 313
3 124 NA 124
4 NA 287 287
5 NA 278 278
合并辅助表
或者,您可以将国家代码和国家名称值存储在单独的表中,并将它们合并到主数据中:
df.countries <- data.frame(
CountryCode = c(109, 124, 313, 287, 278),
CountryName = c('Afghanistan', 'New Zealand', 'Sweden', 'Finland', 'Azerbaijan')
)
CountryCode CountryName
1 109 Afghanistan
2 124 New Zealand
3 313 Sweden
4 287 Finland
5 278 Azerbaijan
c <- c %>%
left_join(df.countries, by = 'CountryCode')
CountryAP CountryEr CountryCode CountryName
<dbl> <dbl> <dbl> <chr>
1 109 NA 109 Afghanistan
2 NA 313 313 Sweden
3 124 NA 124 New Zealand
4 NA 287 287 Finland
5 NA 278 278 Azerbaijan
df.countries我无法重新创建c
。您没有复制所有输出。此外,请尝试在
时查看案例,而不是使用嵌套的ifelse
。感谢您指出这一点,对此表示抱歉。我已经编辑了我的文章以更正c。我无法重新创建c
。您没有复制所有输出。此外,请尝试在
时查看案例,而不是使用嵌套的ifelse
。感谢您指出这一点,对此表示抱歉。我已经编辑了我的文章,修改为c。谢谢。事实证明,当case_接受来自不同列的输入时,它可以一步完成!非常感谢。事实证明,当case_接受来自不同列的输入时,它可以一步完成!