Return multiple values from apply() function in R

∥☆過路亽.° 提交于 2020-12-30 05:35:47

问题


I am wanting to return multiple values from the apply() function and place them in separate columns in R but I keep getting errors. What I am trying to do is this:

experiments$result1, experiments$result2, experiments$result3 <- apply(experiments, 1, 
function(row)
  #Some analysis here
  #return x, y, and z for column result1, result2, and result3
  x, y, z
)

Maybe this is the wrong approach to the problem. Experiments is a data frame with several columns of data. I am wanting to append columns which are the result of the analysis for each row but I don't know how to do that without loops which is not idiomatic for R. Thanks for the help ahead of time.

So here is some more exact code.

experiments$result1, experiments$result2, experiments$result3 <- apply(experiments, 1, function(row)
  x <- row["startingTemp"]*2
  y <- row["startingTemp"]*3
  z <- row["startingTemp"]*4
  return (list(x, y, z))
)

the "startingTemp" field is one of the columns in my "experiments" data frame. I'm getting errors that the type 'closure' is not subsettable and object 'z' not found.


回答1:


If the three values you want to return can be put in a vector (i.e. they are not of some complicated type like the results of a statistical test or a fitted model), just return the vector and apply will bind it to an 3xN matrix.

experiments$result <- apply(experiments, 1, function(row){
  x <- row["startingTemp"]*2
  y <- row["startingTemp"]*3
  z <- row["startingTemp"]*4
  c(x, y, z)
})
experiments$result1 <- experiments$result[1,]
experiments$result2 <- experiments$result[2,]
experiments$result3 <- experiments$result[3,]

If your three return values are of a complicated type (or not scalars) return them as a list (like Alan suggested) and extract them with lapply/sapply.

experiment$result1 <- lapply(experiment$result, "[[", 1)



回答2:


Try return(list(x=x,y=y,z=z)) . For each experiments you return a list of 3 value.

EDIT

The function return ONE list for each experiment.

results <- apply(exp,1,function(x) return(list(x=x,y=x*2,z=x*3))) #results is a list of lists`
experiment$res1 <- unlist(results)[attr(unlist(results),"names")=="x"]
experiment$res2 <- unlist(results)[attr(unlist(results),"names")=="y"]
experiment$res3 <- unlist(results)[attr(unlist(results),"names")=="z"]


来源:https://stackoverflow.com/questions/12300565/return-multiple-values-from-apply-function-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!