Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

r - populate values from another data frame based on predefined set of columns

I have two data frames. The first one look like that:

df1 <- data.frame(Hugo_Symbol=c("CDKN2A", "JUN", "IRS2","MTOR",
                           "NRAS"),
                  A183=c(-0.19,NA,2.01,0.4,1.23),
                  A185=c(0.11,2.45,NA,NA,1.67),
                  A186=c(1.19,NA,2.41,0.78,1.93),
                  A187=c(2.78,NA,NA,0.7,2.23),
                  A188=c(NA,NA,NA,2.4,1.23))
head(df1)
  Hugo_Symbol  A183 A185 A186 A187 A188
1      CDKN2A -0.19 0.11 1.19 2.78   NA
2         JUN    NA 2.45   NA   NA   NA
3        IRS2  2.01   NA 2.41   NA   NA
4        MTOR  0.40   NA 0.78 0.70 2.40
5        NRAS  1.23 1.67 1.93 2.23 1.23

The second data frame is smaller and have an empty values:

df2 <- data.frame(Hugo_Symbol=c("CDKN2A", "IRS2", "NRAS"),
                  A183=c(0, 0, 0),
                  A187=c(0, 0, 0),
                  A188=c(0, 0, 0))
head(df2)
  Hugo_Symbol A183 A187 A188
1      CDKN2A    0    0    0
2        IRS2    0    0    0
3        NRAS    0    0    0

I would like to populate the second data frame with values from the first data frame. The final result will look like that:

  Hugo_Symbol  A183 A187 A188
1      CDKN2A -0.19 2.78 NA
2        IRS2  2.01 NA   NA
3        NRAS  1.23 2.23 1.23

I tried cbind() and merge() functions, but they do not work on data with different number of raws and columns.

I would appreciate any help!

Thank you! Olha

question from:https://stackoverflow.com/questions/66053217/populate-values-from-another-data-frame-based-on-predefined-set-of-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use semi_join from dplyr: your final table has unexpected values. my version:

library(dplyr)
df3 <- df1 %>%  semi_join(df2, by="Hugo_Symbol") %>% 
  select(Hugo_Symbol, A183, A187, A188)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...