First, you should initialize density if you want to do that manual assignment.
densities <- list()
Second, you use the density function in a funny way. You should specify the function different in your lapply. Either you give the function and the extra arguments after the comma, or you construct your own custom little function in the 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 returns a list automatically.
To get the data out of it, you simply use the list indices:
densities[[1]]$x
If you named your list before, you could use the names as well :
names(ff) <- c("data.1","data.2")
densities <- lapply(ff,function(i) {density(i$X2)})
densities[['data.1']]$x
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…