问题 df1 = data.frame(id=c('A1','2','B3','4','5','6','7','8','9','10'),s1c1=c(0,0.2,0,0.5,0.8,0,0,0,0,0),s1c2=c(0,0,0.3,0,0,0.9,0.3,0,0,0),s1c3=c(0.1,0,0,0,0,0,0,0.2,0.8,0.1)) df2 = data.frame(id=c('A1','2','B3','4','5','6','7','8','9','10'),s2c1=c(0,0.22,0,0.35,0.8,0,0,0,0,0),s2c2=c(0,0,0.23,0,0,0.7,0.3,0,0,0),s2c3=c(0.2,0,0,0,0,0,0,0.4,0.9,0.4)) df <- merge(df1,df2, by="id",all=TRUE) df$class <- c(0,0,0,0,0,1,1,0,0,0) > df id s1c1 s1c2 s1c3 s2c1 s2c2 s2c3 class 10 0.0 0.0 0.1 0.00 0.00 0.4 0 2 0