I'm currently trying to add multiple columns to my dataframe using mutate.
This is what I started with:
new_df <- old_df %>%
mutate(demo = c("audience 1", "audience 2")
Here's what I wanted to happen
game demo
1 audience 1
1 audience 2
2 audience 1
2 audience 2
However this just results in an error. I know this is just my bad beginner syntax, can someone help?
CodePudding user response:
I'm assuming your old_df
has just rows, and you'd like to add both values of demo
for each value of game
. If I've got that right, you can use tidyr::expand_grid():
library(tidyr)
old_df <- data.frame(game = 1:2)
old_df %>%
expand_grid(demo = c("audience 1", "audience 2"))
#> # A tibble: 4 × 2
#> game demo
#> <int> <chr>
#> 1 1 audience 1
#> 2 1 audience 2
#> 3 2 audience 1
#> 4 2 audience 2
Created on 2022-10-27 with reprex v2.0.2
CodePudding user response:
Lets start with a reprex:
library(dplyr)
data("mtcars")
old_df = mtcars
new_df <- old_df %>%
mutate(demo = c("audience 1", "audience 2")
This fails, because you are missing a parenthesis to close the mutate call:
Error: Incomplete expression:
new_df <- old_df %>%
mutate(demo = c("audience 1", "audience 2")
Fixing this is insufficient though, as mutate is expecting a vector of the same length (no. of rows) as the table:
new_df <- old_df %>%
mutate(demo = c("audience 1", "audience 2"))
Error in mutate(., demo = c("audience 1", "audience 2")) :
✖ `demo` must be size 32 or 1, not 2.
I am guessing you want it to cycle through audience 1 and 2 for all rows. Just be careful that the total number of rows is even and not odd:
pattern = c("audience 1", "audience 2")
new_df <- old_df %>%
mutate(demo = rep(pattern, length.out = nrow(old_df)))
new_df %>%
select(1:3, demo)
In essence, this repeats the pattern to fill the length of length.out
. Hope this helps!
CodePudding user response:
The answers here are good ones, but I'll put the following solution here because it is simple, and when working with data I've often been surprised how useful a careful application of merge()
from base R can be.
old_df <- data.frame(game = 1:2)
demo_df <- data.frame(demo = c("audience 1", "audience 2"))
merge(demo_df, old_df)
demo game
1 audience 1 1
2 audience 2 1
3 audience 1 2
4 audience 2 2