3
下面是一個簡單的數據和代碼,我想測試:在R中有一個有效的替代方案嗎?
mydata<-structure(list(mpg = c(21, 21, 22.8, 21.4), cyl = c(6, 6, 4,
6), disp = c(160, 160, 108, 258), hp = c(110, 110, 93, 110)), .Names = c("mpg",
"cyl", "disp", "hp"), class = "data.frame", row.names = c("Mazda RX4",
"Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive"))
for(i in 1:4){
show(mydata[i,1]*(mydata[i,2]/mydata[i,3])-mydata[i,4])
}
[1] -109.2125
[1] -109.2125
[1] -92.15556
[1] -109.5023
mapply(function(x,y,w,z){x*(y/w)-z},as.list(mydata[1]),as.list(mydata[2]),as.list(mydata[3]),as.list(mydata[4]))
mpg
[1,] -109.21250
[2,] -109.21250
[3,] -92.15556
[4,] -109.50233
我不知道我是否真的應該在這種情況下使用mapply
。是否有更有效的方式來執行操作?
我怎麼錯過呢?非常感謝! – Metrics