I have a two column dataframe in R and i want to add a third column with values that are a function of the first two.
As long as the function is a simple mathematical formula no problem
data$c <- data$a*15 - 10 + data$b*0.5
But my function is non continous and contains conditionals, i would like to do:
foo <- function(x,y){if (x<2) {return(0)} else {....}
and
data$c <- foo(data$a, data$b)
But this gets me errors. What am i missing?
问问题
155 次
2 回答
2
您正在寻找ifelse
功能。首先创建一个数据框:
> d <- data.frame(x = rnorm(5), y=rnorm(5))
> d
x y
1 -0.3581471 0.18149804
2 0.5096839 0.01260114
3 0.6742558 1.04851531
4 1.0761349 -0.17504221
5 -0.2373933 -1.11475886
现在使用创建一个额外的列ifelse
:
> d$z <- ifelse(d$x < 0, 0, d$y + 1)
> d
x y z
1 -0.3581471 0.18149804 0.0000000
2 0.5096839 0.01260114 1.0126011
3 0.6742558 1.04851531 2.0485153
4 1.0761349 -0.17504221 0.8249578
5 -0.2373933 -1.11475886 0.0000000
于 2012-06-22T10:59:28.413 回答
0
df = data.frame(a=rnorm(20),b=runif(20))
new_var = function(x,y)
{
if(x<0)
{ x = x^2}
else
{ x = x^3}
if(y<.5)
{ -log10(y)}
else
{
exp(y)
}
return((2*(x+y))/x*y)
}
df$c = sapply(1:length(df$a), function(i){new_var(df$a[i],df$b[i])})
head(df)
a b c
1 -0.09149107 0.04834675 0.65517298
2 -2.43732351 0.04086087 0.08228385
3 -1.69573283 0.54795413 1.30474339
4 -0.40106220 0.72952356 8.07641367
5 -0.38244706 0.65524147 7.18118555
6 0.76334945 0.38455655 1.43404971
于 2012-06-22T11:03:55.073 回答