简体   繁体   中英

How to avoid for-loops in R

I guess this is a very usual question amongst R user, but stills not clear to me. I want to parse all the elements of column fnl3$aaa and for each of them perform a lookup in column df$aaa. If a match occurs, then append a numeric value at the end of a temp vector. My problem is that this process takes too long to complete. So I was wondering how I could meke it run faster. Any ideas?

cur <- c("")
for (i in 1:unq21) {
   prev <- cur
   for (j in 1:unq11) {
      if (cnt1$aaa[j] == fnl3$aaa[i]) {
         print('MATCH!!!')
         print(cnt1$freq[j])
         print(fnl3$V1[i])
         cur <- append(prev, as.vector(fnl3$V1[i] / cnt1$freq[j]), after = 0)
      }
   }
}

SAMPLE DATASET:

fnl3
    row.names   aaa                                    V1
1   404     1DC8F216-9138-4151-ABD6-36C3C2C75001        3
2   1533    638DF397-359E-43A5-A2F7-2C43CABA93DA        3
3   14      015ee60dbf299f5419eed89214b7409a            2
4   98      08CFF963-5565-4B8C-814E-FDFA5D37DCD6        2
5   488     226afbbac8dfd6f3c27cb16f9d7922a2            2

cnt1
    aaa                                         freq
1   000089f457881d57d4f221948c2b808c            1
2   00081dd2fd542a2a9c64a8990a1fc986601ab318    1
3   0021a8971f976743c2043b60e38eab46            1
4   0034d5d368611e33d7cfcda85df96eba            1
5   00379FA3-07A6-4AF7-ACBC-721E2E33DD67        1

First, use merge to build a data.frame where the aaa columns match:

m <- merge(fnl3, cnt1, by = "aaa")

Then, you can compute that vector you were storing in cur by simply doing:

with(m, V1 / freq)

You can do it all in one call:

with(merge(fnl3, cnt1, by = "aaa"), V1 / freq)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM