R:如何在下面的代码中使用应用函数而不是循环
R: How to use apply function in below code instead of loop
我需要在我的代码中使用任何应用函数而不是 for loop.Below 提到的是示例代码和函数。
这是我的数据
testdata<-data.table(x1=sample(1000),y1=sample(1000),x2=sample(1000),y2=sample(1000),h=sample(1000))
这是我的函数
testfunction<-function(x0,y0,x1,y1,x2,y2,h){
x<-x0+x1+x2+y0+y1+y2
y<-x+x0+y0
d<-x+y
R<-x+y+d
result <- data.frame(d,R,x,y)
return (result)
}
我当前的 for 循环代码是
resultdf<-data.frame(d=NA,R=NA,x=NA,y=NA)
for (i in 1:nrow(reqdata.LN)){
resultdf[i,]<-testfunction(x0=1.2,y0=2.1,testdata$x1[i],testdata$x2[i],
testdata$y1[i],testdata$y2[i],testdata$h[i])
}
我试过使用 lapply 并应用,但几乎没有错误
bb<-lapply(1:nrow(testdata),testfunction,x0=1.2,y0=2.1,testdata$x1[i],testdata$x2[i],
testdata$y1[i],testdata$y2[i],testdata$h[i])
有人能告诉我如何使用这个应用函数吗?
因为我们有一个 data.table
,可以直接应用该函数,因为 +
是向量化的。
res <- testdata[, testfunction(x0=1.2, y0=2.1, x1, y1, x2, y2, h)]
head(res)
# d R x y
#1 3923.9 7847.8 1960.3 1963.6
#2 2689.9 5379.8 1343.3 1346.6
#3 4523.9 9047.8 2260.3 2263.6
#4 3535.9 7071.8 1766.3 1769.6
#5 3183.9 6367.8 1590.3 1593.6
#6 3677.9 7355.8 1837.3 1840.6
注意:OP post return 中的函数是 data.frame
。当我们使用 data.table
时,最好将它 return 设置为 list
这样它将是一个 data.table 对象
testfunction<-function(x0,y0,x1,y1,x2,y2,h){
x<-x0+x1+x2+y0+y1+y2
y<-x+x0+y0
d<-x+y
R<-x+y+d
list(d= d,R=R,x= x,y = y)
}
head(testdata[, testfunction(x0=1.2, y0=2.1, x1, y1, x2, y2, h)])
# d R x y
#1: 3923.9 7847.8 1960.3 1963.6
#2: 2689.9 5379.8 1343.3 1346.6
#3: 4523.9 9047.8 2260.3 2263.6
#4: 3535.9 7071.8 1766.3 1769.6
#5: 3183.9 6367.8 1590.3 1593.6
#6: 3677.9 7355.8 1837.3 1840.6
我需要在我的代码中使用任何应用函数而不是 for loop.Below 提到的是示例代码和函数。
这是我的数据
testdata<-data.table(x1=sample(1000),y1=sample(1000),x2=sample(1000),y2=sample(1000),h=sample(1000))
这是我的函数
testfunction<-function(x0,y0,x1,y1,x2,y2,h){
x<-x0+x1+x2+y0+y1+y2
y<-x+x0+y0
d<-x+y
R<-x+y+d
result <- data.frame(d,R,x,y)
return (result)
}
我当前的 for 循环代码是
resultdf<-data.frame(d=NA,R=NA,x=NA,y=NA)
for (i in 1:nrow(reqdata.LN)){
resultdf[i,]<-testfunction(x0=1.2,y0=2.1,testdata$x1[i],testdata$x2[i],
testdata$y1[i],testdata$y2[i],testdata$h[i])
}
我试过使用 lapply 并应用,但几乎没有错误
bb<-lapply(1:nrow(testdata),testfunction,x0=1.2,y0=2.1,testdata$x1[i],testdata$x2[i],
testdata$y1[i],testdata$y2[i],testdata$h[i])
有人能告诉我如何使用这个应用函数吗?
因为我们有一个 data.table
,可以直接应用该函数,因为 +
是向量化的。
res <- testdata[, testfunction(x0=1.2, y0=2.1, x1, y1, x2, y2, h)]
head(res)
# d R x y
#1 3923.9 7847.8 1960.3 1963.6
#2 2689.9 5379.8 1343.3 1346.6
#3 4523.9 9047.8 2260.3 2263.6
#4 3535.9 7071.8 1766.3 1769.6
#5 3183.9 6367.8 1590.3 1593.6
#6 3677.9 7355.8 1837.3 1840.6
注意:OP post return 中的函数是 data.frame
。当我们使用 data.table
时,最好将它 return 设置为 list
这样它将是一个 data.table 对象
testfunction<-function(x0,y0,x1,y1,x2,y2,h){
x<-x0+x1+x2+y0+y1+y2
y<-x+x0+y0
d<-x+y
R<-x+y+d
list(d= d,R=R,x= x,y = y)
}
head(testdata[, testfunction(x0=1.2, y0=2.1, x1, y1, x2, y2, h)])
# d R x y
#1: 3923.9 7847.8 1960.3 1963.6
#2: 2689.9 5379.8 1343.3 1346.6
#3: 4523.9 9047.8 2260.3 2263.6
#4: 3535.9 7071.8 1766.3 1769.6
#5: 3183.9 6367.8 1590.3 1593.6
#6: 3677.9 7355.8 1837.3 1840.6