I would like to use the ifelse
function to attribute new values
to a set of columns
in a df
. The real df
has >200 columns
, so I can't state the names of the columns
, instead, I need to specify the interval of columns
(e.g. 2:205
).
df <- data.frame(x=c(123, 121, 120, 124, 125), y=c(1, 0, 0, 0, 1), z=c(0,0, 0, 0, 1))
df
x y z
1 123 1 0
2 121 0 0
3 120 0 0
4 124 0 0
5 125 1 1
However, when I do specify the interval of columns, the first column
is skipped. How can I keep the first column
unchanged?
df2 <- as.data.frame(ifelse(df[2:3] == 1, 2, 0))
df2
y z
1 2 0
2 0 0
3 0 0
4 0 0
5 2 2
Thanks.
Aucun commentaire:
Enregistrer un commentaire