This is one way:
f <- function(x, y){
Z <- ifelse(x==y, 3, 4)
return(Z)
}
DS[3] <- with(DS, f(X,Y))
colnames(DS)[3] <- "Z"
But you don't really need a function to do that.
DS[3] <- with(DS, ifelse(X==Y, 3, 4)) # this works just fine
I'm glad you've decided to use R; eventually you will ne
Dear all,
Suppose I have a dataset with two variables:
X = c(0, 1, 2)
Y = c(1, 1, 1)
DS = data.frame(X, Y)
Now, I want to create a new variable Z with 3 observations, but I want its
values to be the result of a function. I want to create a function that
compares X and Y, and if X = Y, then Z va
2 matches
Mail list logo