使用函数

时间:2016-03-15 14:09:34

标签: r

R相对较新我希望用它来研究更好地理解函数语法和结构。我已阅读the UCLA tutorial,但我没有看到数据帧操作。我有ifelse创建一个新列,然后乘以数据集权重。这将重复多次,所以我试着写一个函数。任何帮助,关键或支持,总是赞赏。

下面的手动代码工作正常(底部输出)。

# Step 2
f$health <- ifelse(f$qintrvyr == 2014 & f$qintrvmo < 4, f$healthcq,
    ifelse(f$qintrvyr == 2015 & f$qintrvmo < 4, f$healthpq,
         f$healthpq + f$healthcq) )

# Step 3
f$myvar <- f$health * f$finlwt21

然而,当我尝试复制时,我显然做错了,因为我收到以下错误:

Error in ifelse(df$qintrvyr == 2014 & df$qintrvmo < 4, df$oldvarcq, ifelse(df$qintrvyr ==  : 
  replacement has length zero
In addition: Warning message:
In rep(yes, length.out = length(ans)) :
  'x' is NULL so the result will be NULL

这是功能:

convert <- function(data, oldvarcq, oldvarpq, newvar){
  df <- data
  df$newvar <- ifelse(df$qintrvyr == 2014 & df$qintrvmo < 4, df$oldvarcq,
                   ifelse(df$qintrvyr == 2015 & df$qintrvmo < 4, df$oldvarpq,
                          df$oldvarpq + df$oldvarcq) )
  df$newvar <- df$newvar * df$finlwt21
  return(df)
}
# test the function
test <- convert(f, foodcq, foodpq, food_wt_spend)

这是使用df列的部分示例所需的输出(其中myvarfood_wt_spendhealthcqfoodcq ...):< / p>

health qintrvyr qintrvmo healthcq healthpq
1 2205.0     2014        4        0   2205.0
2  556.0     2014        4        0    556.0
3   72.0     2014        4        0     72.0
4 1604.4     2014        4        0   1604.4
5 2030.0     2014        4        0   2030.0
6 1080.0     2014        4        0   1080.0
7  314.7     2014        4        0    314.7
     myvar popweight finlwt21
1 42948485  4869.443 19477.77
2 13014121  5851.673 23406.69
3  1415889  4916.281 19665.12
4 20500000  3194.340 12777.36
5 40247923  4956.641 19826.56
6 18055765  4179.575 16718.30
7  6273869  4984.007 19936.03

1 个答案:

答案 0 :(得分:1)

感谢@joran并指导我?Extract以下工作:

convert <- function(data, oldvarcq, oldvarpq, newvar){
  df <- data
  df[[newvar]] <- ifelse(df$qintrvyr == 2014 & df$qintrvmo < 4, df[[oldvarcq]],
                      ifelse(df$qintrvyr == 2015 & df$qintrvmo < 4, df[[oldvarpq]],
                             df[[oldvarpq]] + df[[oldvarcq]]) )
  df[[newvar]] <- df[[newvar]] * df$finlwt21
  return(df)
}