Reputation: 113
I have in my data frame columns for differents regions like this :
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"))
I would like to remove all region column and keep a new column named "region" with the name of the region like that :
data.frame(value=c(1,2,3,4,5),region=c("europe","am_north","am_south","moaf","asia"))
I know there is a package for that like tidy or reshape, but i don't find to do it like this in a easy and simple way
Upvotes: 0
Views: 69
Reputation: 5673
and with 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
Upvotes: 0
Reputation: 6223
Using 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
Upvotes: 2
Reputation: 8072
If you're wanting to do this to arrange the data based off of different conditions in the data frame, then here is a tidyverse
approach.
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
Upvotes: 3