I have a data frame like the following:
mydf <- data.frame(letter=LETTERS[1:10],
val1=sample(1:1000, 10, replace=F),
val2=sample(1:1000, 10, replace=F))
> mydf
letter val1 val2
1 A 877 250
2 B 554 427
3 C 747 92
4 D 353 890
5 E 957 194
6 F 593 338
7 G 723 731
8 H 218 849
9 I 585 932
10 J 873 642
I just want to make a new column which (for this MWE) is equal to one of the value columns, depending on a choose variable, and I wanted to use ifelse for this purpose.
I do this:
choose <- 1
mydf$chosen <- ifelse(choose==1, mydf$val1, mydf$val2)
mydf
But it just uses the first value of val1 for the chosen column... what am I doing wrong here? Thanks
> mydf
letter val1 val2 chosen
1 A 878 984 878
2 B 880 80 878
3 C 296 999 878
4 D 558 230 878
5 E 112 414 878
6 F 132 450 878
7 G 429 693 878
8 H 608 89 878
9 I 409 50 878
10 J 974 980 878
Aucun commentaire:
Enregistrer un commentaire