Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R alternative to "for loops"?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use base merge? Should do what you need.

ID <- c(12,44,56,16,18,29)
people <- data.frame(ID)

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)

merge(people, 
      info, 
      by.x = "ID", 
      by.y = "ID2")
#>   ID age sex
#> 1 12  34   f
#> 2 16  55   m
#> 3 18  23   f
#> 4 29  32   m
#> 5 44  12   f
#> 6 56  43   m
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading