Creatng new variable based upon conditions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Creatng new variable based upon conditions

reichmaj
Given

x <- c(3,2,4,3,5,4,3,2,4,5)
y <- c("A","B","B","A","A","A","A","B","A","B")
xy <- cbind(x,y)

and am wanting to create a new variable "w" where if y=="A" then w==x*10 else w==x*15 such that I end up with a dataframe

      x   y  w
 [1,] 3 "A" 30
 [2,] 2 "B" 30
 [3,] 4 "B" 60
 [4,] 3 "A" 30
 [5,] 5 "A" 50
 [6,] 4 "A" 40
 [7,] 3 "A" 30
 [8,] 2 "B" 30
 [9,] 4 "A" 40
[10,] 5 "B" 75

ifelse, if then, or for loop

Jeff

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.