First, you must initialize the density if you want to perform this manual assignment.
densities <- list()
Secondly, you use the density function funny. You must specify the function in different ways. Either you provide a function and additional decimal arguments, or you create your own custom small function in a Lapply call, as shown below.
data.1 <- data.frame( X1 = letters[1:10], X2 = 1:10 ) data.2 <- data.frame( X1 = letters[11:20], X2 = 10:1 ) ff <- list(data.1,data.2) densities <- lapply(ff,function(i) {density(i$X2)})
This automatically returns a list.
To get data from it, you simply use list indices:
densities[[1]]$x
If you have named your list before, you can also use the names:
names(ff) <- c("data.1","data.2") densities <- lapply(ff,function(i) {density(i$X2)}) densities[['data.1']]$x
Joris meys
source share