Al14 Al14 - 2 years ago 109
R Question

Store the results of apply function in new column

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
A
and does not create column
C
?

Answer Source

We don't need an apply loop for a single column, it can be done with

 df$C  <- ifelse(df[,1] > 2000, 2000, df[,1])

In the OP's code, the output is a matrix with a single column and it also have the same column name resulting in a matrix column with dimnames as 'A'

str(df)
#'data.frame':   4 obs. of  3 variables:
#$ A: num  1999 1999 2001 2001
#$ B: Factor w/ 2 levels "A","D": 1 2 1 2
#$ C: num [1:4, 1] 1999 1999 2000 2000
# ..- attr(*, "dimnames")=List of 2
#  .. ..$ : NULL
#  .. ..$ : chr "A"

So, the column is already there, but it is a matrix

df$C
#      A
#[1,] 1999
#[2,] 1999
#[3,] 2000
#[4,] 2000

Instead of assigning a matrix, it should be converted to a vector

df$C <- as.vector(apply(df[1], 2, function(x) ifelse(x > 2000, 2000, x)))
df$C
#[1] 1999 1999 2000 2000
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download