我正在尝试清理此代码并且想知道是否有任何人对如何在没有循环的R中运行此代码有任何建议。我有一个名为data的数据集,包含100个变量和200,000个观测值。我想要做的主要是通过将每个观察值乘以特定标量来扩展数据集,然后将数据组合在一起。最后,我需要一个包含800,000个观测值的数据集(我有四个要创建的类别)和101个变量。这是我写的一个循环,但这样做非常低效,我想要更快更有效的东西。
datanew <- c()
for (i in 1:51){
for (k in 1:6){
for (m in 1:4){
sub <- subset(data,data$var1==i & data$var2==k)
sub[,4:(ncol(sub)-1)] <- filingstat0711[i,k,m]*sub[,4:(ncol(sub)-1)]
sub$newvar <- m
datanew <- rbind(datanew,sub)
}
}
}
请让我知道您的想法并感谢您的帮助。
以下是一些带有2K观测值的样本数据,而不是200K
# SAMPLE DATA
#------------------------------------------------#
mydf <- as.data.frame(matrix(rnorm(100 * 20e2), ncol=20e2, nrow=100))
var1 <- c(sapply(seq(41), function(x) sample(1:51)))[1:20e2]
var2 <- c(sapply(seq(2 + 20e2/6), function(x) sample(1:6)))[1:20e2]
#----------------------------------#
mydf <- cbind(var1, var2, round(mydf[3:100]*2.5, 2))
filingstat0711 <- array(round(rnorm(51*6*4)*1.5 + abs(rnorm(2)*10)), dim=c(51,6,4))
#------------------------------------------------#
答案 0 :(得分:1)
您可以尝试以下操作。请注意,我们通过调用mapply
替换了前两个for循环,而第三个for循环调用了lapply。
此外,我们正在创建两个矢量,我们将结合矢量化乘法。
# create a table of the i-k index combinations using `expand.grid`
ixk <- expand.grid(i=1:51, k=1:6)
# Take a look at what expand.grid does
head(ixk, 60)
# create two vectors for multiplying against our dataframe subset
multpVec <- c(rep(c(0, 1), times=c(4, ncol(mydf)-4-1)), 0)
invVec <- !multpVec
# example of how we will use the vectors
(multpVec * filingstat0711[1, 2, 1] + invVec)
# Instead of for loops, we can use mapply.
newdf <-
mapply(function(i, k)
# The function that you are `mapply`ing is:
# rbingd'ing a list of dataframes, which were subsetted by matching var1 & var2
# and then multiplying by a value in filingstat
do.call(rbind,
# iterating over m
lapply(1:4, function(m)
# the cbind is for adding the newvar=m, at the end of the subtable
cbind(
# we transpose twice: first the subset to multiply our vector.
# Then the result, to get back our orignal form
t( t(subset(mydf, var1==i & mydf$var2==k)) *
(multpVec * filingstat0711[i,k,m] + invVec)),
# this is an argument to cbind
"newvar"=m)
)),
# the two lists you are passing as arguments are the columns of the expanded grid
ixk$i, ixk$k, SIMPLIFY=FALSE
)
# flatten the data frame
newdf <- do.call(rbind, newdf)
(1)尽量不要使用常见功能data
,table
,df
,sub
等词语
在上面的代码中,我使用mydf
代替data
。
(2)您可以使用apply(ixk, 1, fu..)
代替我使用的mapply
,但我认为mapply可以在这种情况下提供更清晰的代码
祝你好运,欢迎来到SO