给定两个数据框
old.df = data.frame(SampleNo=c('A1', 'B4', 'C5', 'D4'), Result=c(rep("Successful",4)), NoUnit = c(rep(4,4)))
new.df = data.frame(SampleNo=c('A1', 'C5', 'D4', 'E4'), Result=c(rep("Successful",2),rep( "Failure",2)),State=c(rep("California",2),rep("New York",2)))
使其具有以下格式:
> old.df
SampleNo Result NoUnit
1 A1 Successful 4
2 B4 Successful 4
3 C5 Successful 4
4 D4 Successful 4
> new.df
SampleNo Result State
1 A1 Successful California
2 C5 Successful California
3 D4 Failure New York
4 E4 Failure New York
我想用 new.df 中的新数据更新 old.df 的内容,维护 old.df 的行连续性并从 new.df 添加新列。结果 data.frame 将是:
SampleNo Result NoUnit State
1 A1 Successful 4 California
2 B4 Successful 4 <NA>
3 C5 Successful 4 California
4 D4 Failure 4 New York
5 E4 Failure NA New York