library(data.table)
#This is the raw dataframe
(dt <- data.table(name=c('Tom','Tom','Tom','Jake','Jake','Jake'),
math=c(NA,70,NA,45,NA,NA),
music=c(20,NA,NA,NA,NA,65),
english=c(NA,NA,85,NA,30,NA)))
#> name math music english
#> 1: Tom NA 20 NA
#> 2: Tom 70 NA NA
#> 3: Tom NA NA 85
#> 4: Jake 45 NA NA
#> 5: Jake NA NA 30
#> 6: Jake NA 65 NA
# This is target dataframe
(target_dt <- data.table(name=c('Tom','Jake'),
math=c(70,45),
music=c(20,65),
english=c(85,30)))
#> name math music english
#> 1: Tom 70 20 85
#> 2: Jake 45 65 30
由reprex package(v0.2.0)于2018-09-06创建。
有没有简单的方法可以做到这一点?