1

If I have something like this:

id date        type     location
1  1/2/2021    student   Georgia
1                        Maine
1                        New York
2  2/20/2021   teacher   Florida 
2                        Louisiana 
3  2/15/2021   student   New York
3                        Maine
3                        California
3                        Florida

I want to copy the values in the "date" and "type" column based on the value for "id":

id date        type     location
1  1/2/2021    student   Georgia
1  1/2/2001    student   Maine
1  1/2/2001    student   New York
2  2/20/2021   teacher   Florida 
2  2/20/2021   teacher   Louisiana 
3  2/15/2021   student   New York
3  2/15/2021   student   Maine
3  2/15/2021   student   California
3  2/15/2021   student   Florida

I know this has probably asked but I can't figure out how to ask the question. Thanks for your help!

alex
  • 858
  • 5
  • 14

2 Answers2

2

We can convert the blanks "" to NA and use fill

library(dplyr)
library(tidyr)
df1 %>%
    mutate(across(c(date, type), na_if, "")) %>%
    group_by(id) %>%
    fill(date, type)

Or another option is to change it to first element by 'id'

df %>%
   group_by(id) %>%
   mutate(across(c(date, type), first))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Here is a data.table option

> setDT(df)[, lapply(.SD, function(x) replace(x, is.na(x), na.omit(x))), id]
   id      date    type   location
1:  1  1/2/2021 student    Georgia
2:  1  1/2/2021 student      Maine
3:  1  1/2/2021 student   New York
4:  2 2/20/2021 teacher    Florida
5:  2 2/20/2021 teacher  Louisiana
6:  3 2/15/2021 student   New York
7:  3 2/15/2021 student      Maine
8:  3 2/15/2021 student California
9:  3 2/15/2021 student    Florida

Data

> dput(df)
structure(list(id = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L), date = c("1/2/2021",
NA, NA, "2/20/2021", NA, "2/15/2021", NA, NA, NA), type = c("student",
NA, NA, "teacher", NA, "student", NA, NA, NA), location = c("Georgia",
"Maine", "New York", "Florida", "Louisiana", "New York", "Maine",
"California", "Florida")), class = "data.frame", row.names = c(NA,
-9L))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81