在ddply中插入strmacro而不是function

时间:2014-08-11 07:23:51

标签: r plyr

我在R中写了一个strmacro函数,现在我想在一个字符串数组上做这个strmacro。因此,我尝试使用ddply并插入strmacro函数而不是常规函数,但我没有成功。 我希望你能帮助我。

感谢, 阿米特

1 个答案:

答案 0 :(得分:0)

我不知道问题所在,但这是一个使用strmacroddply的工作示例:

# example from ?strmacro
setNA <- strmacro(df, var, values,
                  expr={
                    df$var[df$var %in% values] <- NA
                  })
# create example data using 999 as a missing value indicator
d <- data.frame(
  Grp=c("Trt", "Ctl", "Ctl", "Trt", "Ctl", "Ctl", "Trt", "Ctl", "Trt", "Ctl"),
  V1=c(1, 2, 3, 4, 5, 6, 999, 8,   9,  10),
  V2=c(1, 1, 1, 1, 1, 2, 999, 2, 999, 999)
)
# ddply call
ddply(d, .(Grp), function(x){
  setNA(x, V1, 999)
  x
  })