[R] A calculation in data.frame
arun
smartpink111 at yahoo.com
Tue Jan 7 22:37:32 CET 2014
HI,
May be this helps:
library(reshape2)
df1 <- dcast(DF,A2~A1,value.var="A3")
z <- function(a,b,c){a+2*b+c}
within(df1, newCol <- z(a,b,c))
# A2 a b c newCol
#1 m 1 4 7 16
#2 n 2 5 8 20
#3 p 3 6 9 24
On Tuesday, January 7, 2014 4:15 PM, Ron Michael <ron_michael70 at yahoo.com> wrote:
Hi,
I have to perform some formula driven calculation in a data.frame (as defined below). Let say I have following DF:
> DF <- data.frame(A1 = c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'), A2 = c('m', 'n', 'p', 'm', 'n', 'p', 'm', 'n', 'p'), A3 = c(1,2,3,4,5,6,7,8,9))
> DF
A1 A2 A3
1 a m 1
2 a n 2
3 a p 3
4 b m 4
5 b n 5
6 b p 6
7 c m 7
8 c n 8
9 c p 9
Now let say, user gives one formula which will be applied on the elements of A1 column. Let say the formula looks like:
z = a + 2*b + c (infact the formula will be arbitrary like z = f(a, b, c))
Once such formula is given, the result will be like (for the columns A1, A2, A3 respectively)
z m 16
z n 20
z p 24
the last column comes from the fact that 1 + 2*4 + 7 = 16, 2 + 2*5 + 8 = 20, 3 + 2*6 + 9 = 24
Given that the formula wil be user defined, and to be applied on some data.frame like DF, I am seeking some automated way to accomplice the task for really big DF of previous kind and fairly complex formula.
Can somebody suggest me for efficient way to perform this task in R?
Really appreciate your help.
Thanks,
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list