1

I have the following problem: I have a data frame where the values of different variables of one observation are spread over multiple rows, like this:

Id Var1 Var2
A 0.2 NA
A NA 0.3
B 0.4 NA
B NA 0.5

I tried to convert it in a way, that each observation is in one row:

Var1 Var2
A 0.2 0.3
B 0.4 0.5

But I could´nt come up with a solution... I would very much appreciate it if someone has some ideas on how to solve that.

Sev
  • 9
  • 2
  • One way among others: `df %>% group_by(Id) %>% summarise(across(everything(), ~first(na.omit(.))))` – Maël Mar 18 '22 at 11:55
  • Yes this answers my question, thank you very much! – Sev Mar 18 '22 at 12:04
  • 1
    Don't agree with closing this question, the supposed duplicate requests a tidyverse solution whereas there is a much simpler base solution. – George Savva Mar 18 '22 at 12:05

1 Answers1

0

If you are sure each ID only has one non-missing value per element:

> aggregate( .~Id , data=dat, max, na.rm=TRUE, na.action = NULL)

  Id Var1 Var2
1  A  0.2  0.3
2  B  0.4  0.5

Data:

dat <- read.table(text="Id  Var1    Var2
A   0.2     NA
A   NA  0.3
B   0.4     NA
B   NA  0.5", header=TRUE)
George Savva
  • 4,152
  • 1
  • 7
  • 21