In a problem, I have a set of vectors. Each vector has sensor readings but are of different lengths. I'd like to compute the same descriptive statistics on each of these vectors. My question is, how should I store them in R. Using c()
concatenates the vectors. Using list()
seems to cause functions like mean()
to misbehave. Is a data frame the right object?
What is the best practice for applying the same function to vectors if different sizes? Supposing the data resides in a SQL server, how should it be imported?
Vectors of different sizes should be combined into a list: a data.frame expects each column to be the same length.
Use lapply
to fetch your data. Then use lapply
again to get the descriptive statistics.
x <- lapply(ids, sqlfunction)
stats <- lapply(x, summary)
Where sqlfunction
is some function you created to query your database. You can collapse the stats
list into a data.frame by calling do.call(rbind, stats)
or by using plyr
:
library(plyr)
x <- llply(ids, sqlfunction)
stats <- ldply(x, summary)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With