Store the results of apply function in new column

Viewed 1563

My data below

A<-c(1999,1999, 2001, 2001)
B<-c("A", "D", "A", "D")
df<-data.frame (A, B)

I am applying

df$C<-apply(df[1], 2, function(x) ifelse(x > 2000, 2000, x))

to create a new column with values based on a reference column

Output

     A B    C
1 1999 A 1999
2 1999 D 1999
3 2001 A 2000
4 2001 D 2000

Why it does replicate the same colname Aand does not create column C?

1 Answers
Related