创建多个数据框

时间:2015-03-01 02:41:53

标签: r

我正在尝试使用apply系列创建两个独立的数据框但是在尝试创建它们时遇到了麻烦,如下所示。

X <- c(3,4,3,2,3,4,5,6,7,7,6,5,4,3,3,5,3,6,3,5,6,3,6,3,4,5,5,4,3,4,5,3,5,5,4)
Y <- c(3,2,1,3,4,2,1,2,3,5,4,3,2,1,1,3,4,5,6,7,6,5,4,3,2,3,4,3,4,2,4,3,NA,NA,NA)

mydata<-data.frame(X,Y)

L <- seq(1:length(mydata))
n <- function(x) length(na.omit(mydata[,x]))
n <- lapply(L,n)

# sequence each (Variable time)
x <- function(x) seq(1:n[[x]])
x <- lapply(L,x)
y <- function(x) na.omit(mydata[,x])
y <- lapply(L,y)

# create multiple data frames 
Data <- function (x) data.frame(y[[x]], x[[x]])
Data <- lapply(L,Data)
# Error en x[[x]] : subíndice fuera de  los límites

# what am I doing wrong I can not figure out how to do it using mapply
# However works if I do (when L is 1 and 2 referring to the x and x, I expect outcome equal to runing the two rows below

data.frame(y[[2]], x[[2]])
data.frame(y[[1]], x[[1]])

0 个答案:

没有答案
相关问题