How to use Apply function in a column dependent case?

dsauce

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
grrgrrbla

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!!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use apply function in a filtered matrix column?

From Java

Pandas: How can I use the apply() function for a single column?

From Dev

How can I use the apply() function for a single column?

From Dev

how to use apply function to take no names column mean?

From Dev

Use sample() function to apply in a range of column

From Dev

Haskell: how to use case function

From Dev

How to apply ifelse function by column names?

From Dev

pandas DataFrame, how to apply function to a specific column?

From Dev

Clojure - How to apply a different function to each column?

From Dev

How to apply a function to a column of a Spark DataFrame?

From Dev

how to apply a function on every column of a data?

From Dev

How to apply regex to a column in pandas to find a value and then apply a function to this?

From Dev

How to apply a function to each element of an array when the result is dependent of preceding cell

From Dev

how to use case statement to check if column is null

From Dev

How to use apply for vector with parameterized function?

From Dev

Apply Logic In a MySQL Column with IF or Case

From Dev

How to apply/loop function, in this case chull(), to groups within a dataframe in r?

From Dev

How can I use an NUnit test case that is dependent on TestSetup method having run?

From Dev

How to apply histogram on dependent data in R?

From Dev

How to use dependent: :destroy in rails?

From Dev

Python Pandas: How to make a column row dependent on it's previous rows, possibly with a function?

From Dev

Scala use dependent type in type signature of function?

From Dev

How to properly apply a lambda function into a pandas data frame column

From Dev

How to apply a function on a specific column in the data frame using R?

From Java

How to apply same function to every specified column in a data.table

From Dev

How to apply a function to every value in a column in a pandas dataframe?

From Dev

How to select and apply a function to multiple column within a list of dataframes

From Dev

How to apply a function on a specific column in the data frame using R?

From Dev

jQuery: How to apply blur function to fourth column of each row

Related Related

  1. 1

    How to use apply function in a filtered matrix column?

  2. 2

    Pandas: How can I use the apply() function for a single column?

  3. 3

    How can I use the apply() function for a single column?

  4. 4

    how to use apply function to take no names column mean?

  5. 5

    Use sample() function to apply in a range of column

  6. 6

    Haskell: how to use case function

  7. 7

    How to apply ifelse function by column names?

  8. 8

    pandas DataFrame, how to apply function to a specific column?

  9. 9

    Clojure - How to apply a different function to each column?

  10. 10

    How to apply a function to a column of a Spark DataFrame?

  11. 11

    how to apply a function on every column of a data?

  12. 12

    How to apply regex to a column in pandas to find a value and then apply a function to this?

  13. 13

    How to apply a function to each element of an array when the result is dependent of preceding cell

  14. 14

    how to use case statement to check if column is null

  15. 15

    How to use apply for vector with parameterized function?

  16. 16

    Apply Logic In a MySQL Column with IF or Case

  17. 17

    How to apply/loop function, in this case chull(), to groups within a dataframe in r?

  18. 18

    How can I use an NUnit test case that is dependent on TestSetup method having run?

  19. 19

    How to apply histogram on dependent data in R?

  20. 20

    How to use dependent: :destroy in rails?

  21. 21

    Python Pandas: How to make a column row dependent on it's previous rows, possibly with a function?

  22. 22

    Scala use dependent type in type signature of function?

  23. 23

    How to properly apply a lambda function into a pandas data frame column

  24. 24

    How to apply a function on a specific column in the data frame using R?

  25. 25

    How to apply same function to every specified column in a data.table

  26. 26

    How to apply a function to every value in a column in a pandas dataframe?

  27. 27

    How to select and apply a function to multiple column within a list of dataframes

  28. 28

    How to apply a function on a specific column in the data frame using R?

  29. 29

    jQuery: How to apply blur function to fourth column of each row

HotTag

Archive