I have a table that looks like this:
df <- tribble(
~name, ~point, ~time,
"A", 1, 1,
"B", 1, 1,
"W", 2, 1,
"A", 3, 2,
"B", 1, 2,
"W", 4, 2,
)
# A tibble: 6 x 3
name point time
<chr> <dbl> <dbl>
1 A 1 1
2 B 1 1
3 W 2 1
4 A 3 2
5 B 1 2
6 W 4 2
And I want to turn the value of the point column into the value of every entry in a new column, specifically where the name is equal to W, like so:
# A tibble: 6 x 4
name point time W_point_value
<chr> <dbl> <dbl> <dbl>
1 A 1 1 2
2 B 1 1 2
3 W 2 1 2
4 A 3 2 4
5 B 1 2 4
6 W 4 2 4
I know I can use filter to get the result but I'm struggling to append it into a new column due to improper sizing.
Thank you
CodePudding user response:
You can do this:
df %>%
group_by(time) %>%
mutate(W_point_time = point[name == "W"])
# A tibble: 6 x 4
# Groups: time [2]
name point time W_point_time
<chr> <dbl> <dbl> <dbl>
1 A 1 1 2
2 B 1 1 2
3 W 2 1 2
4 A 3 2 4
5 B 1 2 4
6 W 4 2 4
