I am working in big data frames with a lot of information and I need to get specific information from on to merge it into my main data frame. I made a small example here with smaller samples:
ID <- c(12,44,56,16,18,29)
people <- data.frame(ID)
ID
1 12
2 44
3 56
4 16
5 18
6 29
sex<-c("f","f","m","f","m","m")
age <- c(12,34,55,23,43,32)
ID2 <- c(44,12,16,18,56,29)
info <- data.frame(ID2,age,sex)
ID2 age sex
1 44 12 f
2 12 34 f
3 16 55 m
4 18 23 f
5 56 43 m
6 29 32 m
My goal here is to merge the information of "info" into "people" while considering the ID. For this, I used a for
loop like this:
for (i in 1:nrow(people)) {
for (j in 1:nrow(info)){
if(people$ID[i] == info$ID2[j]){
people$age[i] <- info$age[j]
people$sex[i] <- info$sex[j]
}
}
}
My code works fine but it seems like when I apply it in a bigger sample, the calculation time is very high. Is there an alternative to this loop?