I have two dataframes:
> # Create dataframe1
> ID <- c('ID1','ID2','ID3','ID4','ID5','ID6','ID7')
> hr <- c(56,32,38,NA,42,23,35)
> cr <- c(1,4,5,2,2,5,4)
> data1 <- data.frame(ID,hr,cr)
> data1
ID hr cr
1 ID1 56 1
2 ID2 32 4
3 ID3 38 5
4 ID4 NA 2
5 ID5 42 2
6 ID6 23 5
7 ID7 35 4
>
> # Create dataframe2
> ID <- c('ID1','ID2','ID5','ID7','ID9','ID6','ID7')
> hr <- c(23,13,15,49,22,24,23)
> cr <- c(1,4,5,2,2,5,4)
> data2 <- data.frame(ID,hr,cr)
> data2
ID hr cr
1 ID1 23 1
2 ID2 13 4
3 ID5 15 5
4 ID7 49 2
5 ID9 22 2
6 ID6 24 5
7 ID7 23 4
If the values in columns ID and cr match in both dataframes, I want to paste dataframe2$hr
column onto dataframe 1 (otherwise paste NA), ie. if dataframe1$ID == dataframe2$ID & dataframe1$cr == dataframe2$cr
, then paste the value of dataframe2$hr
into a new column in dataframe1 and if they don't match, paste NA.
What is the best way of doing this? I tried this way:
data1$newhr=ifelse(data1$ID%in%data2$ID2 & data1$cr%in%data2$cr2,data2$hr2,"NA")
which works for this small example, however for my larger dataframes (which vary in dimensions) this does not work. I get the following error message:
Error in
$<-.data.frame
(*tmp*
, "newP", value = c(NA, NA, NA, NA, NA, : replacement has 563 rows, data has 48
In addition, a warning message in gwascat_highlight$V2 %in% testfull3$gcSNP & gwascat_highlight$V3 %in%
:
longer object length is not a multiple of shorter object length
I cannot recreate my larger data frames, however is there an alternative method of carrying out this task that I could try?