我尝试在两个数据帧的所有行和列上应用一个函数,但我不知道如何用 apply 来解决它。我认为以下脚本解释了我打算做什么以及我试图解决它的方式。任何建议将不胜感激!请注意,这simplefunction
只是一个示例函数,以使其保持简单。
# some data and a function
df1<-data.frame(name=c("aa","bb","cc","dd","ee"),a=sample(1:50,5),b=sample(1:50,5),c=sample(1:50,5))
df2<-data.frame(name=c("aa","bb","cc","dd","ee"),a=sample(1:50,5),b=sample(1:50,5),c=sample(1:50,5))
simplefunction<-function(a,b){a+b}
# apply on a single row
simplefunction(df1[1,2],df2[1,2])
# apply over all colums
apply(?)
## apply over all columns and rows
# create df to receive results
df3<-df2
# loop it
for (i in 2:5)df3[i]<-apply(?)