【发布时间】:2018-09-19 02:16:08
【问题描述】:
我有一个针对不同国家和次国家区域的数据集。变量country 确定国家(a、b、c),变量region_country_X 具有该国家不同子区域的数值(对于另一个国家的情况为NA) .数据框见以下代码:
set.seed(6543)
df <- data.frame(country = sample(c("a", "b", "c"), 1000, replace = TRUE),
region_country_a = sample(c(0, 1, 2, 3, 4, 5, 6, 7), 1000, replace = TRUE),
region_country_b = sample(c(0, 1, 2, 3, 4, 5, 6, 7, 8), 1000, replace = TRUE),
region_country_c = sample(c(0, 1, 2, 3), 1000, replace = TRUE))
df$region_country_a <- ifelse(df$country != "a", NA, df$region_country_a)
df$region_country_b <- ifelse(df$country != "b", NA, df$region_country_b)
df$region_country_c <- ifelse(df$country != "c", NA, df$region_country_c)
数据框的头部是这样的:
> head(df, 5)
country region_country_a region_country_b region_country_c
1 c NA NA 1
2 b NA 3 NA
3 a 2 NA NA
4 c NA NA 1
5 b NA 2 NA
我现在想在一个列中添加一个包含所有区域的新变量,但不知道如何最好地解决这个问题。
我希望r 执行以下操作:
- 新增栏目
regions - 遍历列
country和region_country_a、..._b、..._c,并为每个组合获取一个新值(从0开始计数,国家a,地区0向上,添加下一个每个新国家/地区组合的最高数字)。
生成的数据框如下所示:
country regions_country_a regions_country_b regions_country_c regions
1 c NA NA 1 18 #counting with a/0 = 0 etc., a7 = 7, b0 = 8 etc.
2 b NA 3 NA 11
3 a 2 NA NA 2
4 c NA NA 1 18
5 b NA 2 NA 10
我不确定如何最好地解决这个问题,因为我对r 很陌生,有人能指出我正确的方向吗?
【问题讨论】:
标签: r