How to avoid for-loops in R -
i guess usual question amongst r user, stills not clear me. want parse elements of column fnl3$aaa , each of them perform lookup in column df$aaa. if match occurs, append numeric value @ end of temp vector. problem process takes long complete. wondering how meke run faster. ideas?
cur <- c("") (i in 1:unq21) { prev <- cur (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
build data.frame aaa
columns match:
m <- merge(fnl3, cnt1, = "aaa")
then, can compute vector storing in cur
doing:
with(m, v1 / freq)
you can in 1 call:
with(merge(fnl3, cnt1, = "aaa"), v1 / freq)
Comments
Post a Comment