问题描述
这是我要记住的功能-尚无法使用。
genctrlvars <- function(varorder,...) {
for (j in 1:varorder)
{
# print(nameofvar)
# print(varorder)
# print(str(testdf))
testdf[,(ncol(testdf) + 1)] <- -1
testdf[,ncol(testdf)] <- testdf[,nameofvar]^j
existingname <- names(testdf[ncol(testdf)])
newname <- paste0(nameofvar,"_",j)
names(testdf)[names(testdf) == existingname] <- newname
# print(str(testdf))
}
return(testdf)
}
它的名字是这样的:
mymapplytest <- mapply(genctrlvars,varorder,nameofvar,MoreArgs = list(testdf))
这个想法是为两个列表中的每个元素调用函数,即varorder
和nameofvar
-这两个列表都有2个元素。两个列表的数据框都相同。
那么,为什么它不运行?我在做什么错了?
所有参数的详细信息如下所示:
"varorder" is a list of 2 'varorder' values i.e. c(1,1)
"nameofvar" is a list of the names of 2 the variables i.e. c("Trend","Temp")
"testdf" is a data frame - structure is shown below :-
varorder <- list(1,1)
nameofvar <- as.list(gsub("order","",c("Trendorder","Temporder")))
testdf <- list(controlvarsdfobjects[[1]])
str(testdf)
List of 1
$ :'data.frame': 44640 obs. of 6 variables:
..$ Trend : num [1:44640] 1 1 1 1 1 1 1 1 1 1 ...
..$ Temp : num [1:44640] NA 22.8 23 22.8 22.7 22.4 21.3 20.7 20.4 20 ...
..$ Wind : num [1:44640] NA 8 10 10 9 5 2 0 0 0 ...
..$ Humidity : num [1:44640] NA 77 77 77 78 79 82 85 87 88 ...
..$ Sunhrs : num [1:44640] NA 8.82 8.82 8.82 8.82 8.82 8.82 8.82 8.82 8.82 ...
..$ Temp_Recency: num [1:44640] NA NA NA NA NA NA NA NA NA NA ...
这是我在激活打印语句的情况下运行时遇到的错误:
mymapplytest <- mapply(genctrlvars,MoreArgs = list(testdf))
[[1]]
[1] "Trend"
[[2]]
[1] "Temp"
[1] 1
T List of 1
$ :'data.frame': 44640 obs. of 6 variables:
..$ Trend : num [1:44640] 1 1 1 1 1 1 1 1 1 1 ...
..$ Temp : num [1:44640] NA 22.8 23 22.8 22.7 22.4 21.3 20.7 20.4 20 ...
..$ Wind : num [1:44640] NA 8 10 10 9 5 2 0 0 0 ...
..$ Humidity : num [1:44640] NA 77 77 77 78 79 82 85 87 88 ...
..$ Sunhrs : num [1:44640] NA 8.82 8.82 8.82 8.82 8.82 8.82 8.82 8.82 8.82 ...
..$ Temp_Recency: num [1:44640] NA NA NA NA NA NA NA NA NA NA ...
NULL
Error in testdf[,(ncol(testdf) + 1)] <- -1 :
incorrect number of subscripts on matrix
我担心的是,为什么要为第一个元素运行同时打印Trend
和Temp
的列表项-不应该只看第一个元素即{{1 }}?
编辑
我将其作为循环进行了工作,但我更喜欢使用Trend
函数。这是循环版本的工作代码。如果有人可以帮助我将其转换为mapply
形式,将不胜感激。
mapply
这是循环版本最终输出的样子:
orderlist <- list(trendorder,temporder,windorder,humidityorder,sunhrsorder,temp_recencyorder) #pvorder not included yet
names(orderlist) <- list("Trendorder","Temporder","Windorder","Humidityorder","Sunhrsorder","Temp_Recencyorder") #"PVvorder" not included yet
orderlistnames <- as.list(names(orderlist))
for (j in 1:length(orderlist))
{thisvarname <- gsub("order",orderlistnames[[j]])
thisvarorder <- orderlist[[j]]
if (j == 1)
{thisvarorigdfobjects <- testdf
}
for (k in 0:thisvarorder)
{if (k == 0)
{if (thisvarorder == 0)
{thisvarorigdfobjects[,(ncol(thisvarorigdfobjects) + 1)] <- NA
existingname <- names(thisvarorigdfobjects[ncol(thisvarorigdfobjects)])
newname <- paste0(thisvarname,k)
names(thisvarorigdfobjects)[names(thisvarorigdfobjects) == existingname] <- newname
}
else
{k <- k + 1
}
}
else
{thisvarorigdfobjects[,(ncol(thisvarorigdfobjects) + 1)] <- -1
thisvarorigdfobjects[,ncol(thisvarorigdfobjects)] <- thisvarorigdfobjects[,thisvarname]^k
existingname <- names(thisvarorigdfobjects[ncol(thisvarorigdfobjects)])
newname <- paste0(thisvarname,k)
names(thisvarorigdfobjects)[names(thisvarorigdfobjects) == existingname] <- newname
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)