简体   繁体   中英

How to use Apply function in a column dependent case?

I am trying to fill a new column in a df of 700k records and it goes too slow with for loop and therefore want to use apply function. Not familiar with it and below is my attempt but this doesn't work. Please help

myfunc <- function(a,b,c,d) {if (a=="xyz" & b==11) {c=d}}
dataf[,'target'] <- apply(dataf, 1, function(dataf) myfunc(dataf[,'col1'],dataf[,'col2'],dataf[,'target'],dataf[,'col3']))

Adding more description -

What I have:

a   b   c   d
x   2       p
x   2       p
x   2       p
xyz 11      p
xyz 11      p
xyz 2       p
y   2       p
y   2       p
y   2       p

What I want to achieve:

a   b   c   d
x   2       p
x   2       p
x   2       p
xyz 11  p   p
xyz 11  p   p
xyz 2       p
y   2       p
y   2       p
y   2       p

given your OP, I am guessing you want this??

library(data.table)
setDT(dataf)[a == "xyz" & b == 11, c := d]

output:

     a  b d  c
1:   x  2 p NA
2:   x  2 p NA
3:   x  2 p NA
4: xyz 11 p  p
5: xyz 11 p  p
6: xyz  2 p NA
7:   y  2 p NA
8:   y  2 p NA
9:   y  2 p NA

I highly suggest reading the tutorial of data.table which is super-fast and can be used for a lot of different things. On this site you find even more articles . I would read them all, you will need all of this and it will help you a lot!!

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