Reputation: 2611
I needed to create a list out of a data.frame in a row-by-row shape for later conversion to JSON.
I was surprised to see a different behaviour between apply and lapply:
This is an example:
df_ = data.frame( Month = month.abb[1:5],
A=rnorm(5, mean = 5, sd = 4),
B=rnorm(5, mean = 5, sd = 4),
C=rnorm(5, mean = 5, sd = 4),
D=rnorm(5, mean = 5, sd = 4),
E=rnorm(5, mean = 5, sd = 4), stringsAsFactors=F)
df1_ <- lapply(1:nrow(df_), function(i) {l_ <- as.list(df_[i, ])
return(l_)})
df2_ <- apply(df_,1, as.list)
df1_[[1]]
$Month
[1] "Jan"
$A
[1] 8.235366
$B
[1] -0.2163872
$C
[1] 10.75159
$D
[1] 5.401502
$E
[1] 7.175394
df2_[[1]]
$Month
[1] "Jan"
$A
[1] "8.2353656"
$B
[1] "-0.2163872"
$C
[1] "10.751592"
$D
[1] " 5.4015020"
$E
[1] " 7.175394"
As far as I can tell they appear identical, but apply has coerced all numeric to character, while lapply has preserved them as numeric.
I would like to have an understanding of why this happens, to avoid this pitfall in the future.
Upvotes: 0
Views: 32
Reputation: 22293
apply
takes matrix
arguments. data.frame
s will be coerced to matrix
before anything else is done. Hence the conversion of everything to the same type (character
).
lapply
takes list
arguments. Therefore it coerces the data.frame
to a list
and does not have to convert the arguments.
Upvotes: 1