'Conditionally assign values from one dataframe to another [R]
I am learning R. I know how to join dataframes based on various criteria and using various methods - all of that if they have something in common (e.g. column). But what if they do not?
I have a two example dataframes: df_data and df_categories (code provided below). I would like to modify df_data by adding new columns based on the values in df_categories. The dataframes do not share a common column.
#first dataframe (df_category):
group_vec <- c(rep("nerd", 5),
rep("bum", 5),
rep("hipster", 5),
rep("metalhead", 5),
rep("geek", 5),
rep("hooligan", 5))
sample_vec <- c("n1", "n2", "n3", "n4", "n5",
"b1", "b2", "b3", "b4", "b5",
"h1", "h2", "h3", "h4", "h5",
"m1", "m2", "m3", "m4", "m5",
"g1", "g2", "g3", "g4", "g5",
"ho1", "ho2", "ho3", "ho4", "ho5")
df_category <- data.frame(group_vec, sample_vec)
#second dataframe (df_data):
group <- c(rep("A", 2),
rep("B", 5),
rep("C", 4),
rep("D", 3))
value <- c(20, 19, 11, 8, 9, 13, 10, 7, 6, 7, 5, 17, 16, 18)
sample <- c("one1", "two1",
"one2", "two2","thr2", "fou2", "fiv2",
"one3", "two3","thr3", "fou3",
"one4", "two4","thr4")
df_data <- data.frame(group, sample, value)
In a df_data, I would like to create a column named "gr_vec", which would contain values from df_category$group_vec - for each unique value in df_data$group.
In a df_data, I would also like to create a column named "sam_vec", which would contain values from df_category$sample_vec - for each unique value in df_data$sample.
Here is the desired output:
#output dataframe:
group <- c(rep("A", 2),
rep("B", 5),
rep("C", 4),
rep("D", 3))
gr_vec <- c(rep("nerd", 2),
rep("bum", 5),
rep("hipster", 4),
rep("metalhead", 3))
sample <- c("one1", "two1",
"one2", "two2","thr2", "fou2", "fiv2",
"one3", "two3","thr3", "fou3",
"one4", "two4","thr4")
sam_vec <- c("n1", "n2",
"b1", "b2", "b3", "b4", "b5",
"h1", "h2", "h3", "h4",
"m1", "m2", "m3")
value <- c(20, 19, 11, 8, 9, 13, 10, 7, 6, 7, 5, 17, 16, 18)
df_out <- data.frame(group, gr_vec, sample, sam_vec, value)
I tried with match, rle and case_when, but was unable to assign the values correctly. I did not find a similar question, so I posted it here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
