Split data.frame by variable and apply function referring to concrete row

Pepacz

I need to split data.frame by some variable and calculate difference between each row's value and value from some other specified row.

In example below, I split df by v1. Then for each row of v3 calculate difference between the actual value and v3[v2 == "C"].

v1 <- rep(1:4,each = 3)
v2 <- rep(c("A","B","C"),4)
v3 <- rep(1:5,3)[1:12]
res <- c(-2,-1,0,3,4,0,-2,-1,0,3,-1,0)
df <- data.frame(v1,v2,v3,res)

df
   v1 v2 v3 res
1   1  A  1  -2
2   1  B  2  -1
3   1  C  3   0
4   2  A  4   3
5   2  B  5   4
6   2  C  1   0
7   3  A  2  -2
8   3  B  3  -1
9   3  C  4   0
10  4  A  5   3
11  4  B  1  -1
12  4  C  2   0

I prefer plyr or data.table, if possible.

lmo

Here is a data table solution:

library(data.table)
setDT(df)
df[, new := v3 - v3[v2=="C"], by = "v1"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Apply function in data frame

From Dev

Apply custom function to two columns for every row in data frame in R

From Dev

Apply function row wise on pandas data frame on columns with numerical values

From Dev

How to apply custom function to pandas data frame for each row

From Dev

Apply function to subset of data frame

From Dev

Apply a function data frame column

From Dev

R: apply function to each row of a data frame by factor, calling in the function a value (by factor)

From Dev

R: apply function to each row of a data frame by factor, calling in the function a value (by factor)

From Dev

Split data frame into groups by time and apply a function to multiple columns using R

From Dev

R - apply lm on each data frame row

From Dev

Python apply class method to row of data frame

From Dev

R - apply lm on each data frame row

From Dev

R - Split data frame row into two rows

From Dev

How to apply pv.uneven (FinCal library) (or any function with vectors as input) to every row of a data frame?

From Dev

Function arguments referring to the same variable

From Dev

Split, apply, and combine multiple data frames into one data frame

From Dev

How to efficiently apply a two variable function to data.frame( or matrix) elements - to fill an output matrix?

From Dev

R: Apply function on data frame A dependent on values of data frame B

From Dev

How to apply scipy function on Pandas data frame

From Dev

R Apply function on data frame columns

From Dev

apply a function in data frame with if/else statement in R

From Dev

Group by and apply custom function in pandas data frame

From Dev

apply a function in data frame with if/else statement in R

From Dev

Apply function to heterogeneous rows of data.frame

From Dev

Apply function to column of data frame (column is a list)

From Dev

how to apply duplicated function to the columns of a data frame

From Dev

How to sort a data frame by row in R and split into muliple data frames?

From Dev

Pandas Data Frame function application to a specific row

From Dev

Plot a function for every row in an R data frame

Related Related

  1. 1

    Apply function in data frame

  2. 2

    Apply custom function to two columns for every row in data frame in R

  3. 3

    Apply function row wise on pandas data frame on columns with numerical values

  4. 4

    How to apply custom function to pandas data frame for each row

  5. 5

    Apply function to subset of data frame

  6. 6

    Apply a function data frame column

  7. 7

    R: apply function to each row of a data frame by factor, calling in the function a value (by factor)

  8. 8

    R: apply function to each row of a data frame by factor, calling in the function a value (by factor)

  9. 9

    Split data frame into groups by time and apply a function to multiple columns using R

  10. 10

    R - apply lm on each data frame row

  11. 11

    Python apply class method to row of data frame

  12. 12

    R - apply lm on each data frame row

  13. 13

    R - Split data frame row into two rows

  14. 14

    How to apply pv.uneven (FinCal library) (or any function with vectors as input) to every row of a data frame?

  15. 15

    Function arguments referring to the same variable

  16. 16

    Split, apply, and combine multiple data frames into one data frame

  17. 17

    How to efficiently apply a two variable function to data.frame( or matrix) elements - to fill an output matrix?

  18. 18

    R: Apply function on data frame A dependent on values of data frame B

  19. 19

    How to apply scipy function on Pandas data frame

  20. 20

    R Apply function on data frame columns

  21. 21

    apply a function in data frame with if/else statement in R

  22. 22

    Group by and apply custom function in pandas data frame

  23. 23

    apply a function in data frame with if/else statement in R

  24. 24

    Apply function to heterogeneous rows of data.frame

  25. 25

    Apply function to column of data frame (column is a list)

  26. 26

    how to apply duplicated function to the columns of a data frame

  27. 27

    How to sort a data frame by row in R and split into muliple data frames?

  28. 28

    Pandas Data Frame function application to a specific row

  29. 29

    Plot a function for every row in an R data frame

HotTag

Archive