R:在函数中指定dataframe参数

R:在函数中指定dataframe参数,r,function,dataframe,R,Function,Dataframe,我正在尝试向数据帧添加一个新列,但是我需要为大约5个数据帧创建许多列。所以我想写一个函数。因为每个数据帧的所有列都是相同的,所以我想到的是: n = c(2,3,5) f = c("two", "three", "five") q = c(1,1.5,2.5) df= data.frame(n,f,q) fxn_foo <- function(x){ x$egret <- (x$n)/2 } fxn_foo(df) df$egret n=c(2,3,

我正在尝试向数据帧添加一个新列,但是我需要为大约5个数据帧创建许多列。所以我想写一个函数。因为每个数据帧的所有列都是相同的,所以我想到的是:

 n = c(2,3,5)
 f = c("two", "three", "five")
 q = c(1,1.5,2.5)
 df= data.frame(n,f,q)
 fxn_foo <- function(x){
      x$egret <- (x$n)/2
 }

 fxn_foo(df)

 df$egret
n=c(2,3,5)
f=c(“二”、“三”、“五”)
q=c(1,1.5,2.5)
df=数据帧(n,f,q)

fxn_foo您需要一个
return
语句:

fxn_foo <- function(x){
  x$egret <- (x$n)/2
  return(x)
  }

fxn\u foo您需要一个
return
语句:

fxn_foo <- function(x){
  x$egret <- (x$n)/2
  return(x)
  }
fxn\u foo