R在唯一

时间:2018-01-09 19:02:45

标签: r

我的数据框列中有不同的区域,如下所示:

data.frame(value=c(1,2,3,4,5),europe=c("yes","no","no","no","no"),
am_north=c("no","yes","no","no","no"),am_south=c("no","no","yes","no","no"),moaf=c("no","no","no","yes","no"),asia=c("no","no","no","no","yes"))

我想删除所有区域列,并保留一个名为" region"的新列。以及该地区的名称:

data.frame(value=c(1,2,3,4,5),region=c("europe","am_north","am_south","moaf","asia"))

我知道有一个类似于整洁或重塑的包装,但我不会发现这样做的方式简单明了

3 个答案:

答案 0 :(得分:3)

如果您想要根据数据框中的不同条件来安排数据,那么这是一种tidyverse方法。

library(tidyverse)

gather(df, region, response, -value) %>% 
  filter(response == "yes") %>% 
  select(-response)

  value   region
1     1   europe
2     2 am_north
3     3 am_south
4     4     moaf
5     5     asia

答案 1 :(得分:2)

使用base -r:

df[["region"]] = colnames(df)[apply(df == "yes", 1, which)]
df[, c("value", "region")]
# value   region
# 1     1   europe
# 2     2 am_north
# 3     3 am_south
# 4     4     moaf
# 5     5     asia

答案 2 :(得分:0)

和data.table

melt(DT, id.vars = "value",value.name = "answer",variable.name = "region")[answer == "yes",.(value,region)]
   value   region
1:     1   europe
2:     2 am_north
3:     3 am_south
4:     4     moaf
5:     5     asia