R ddply, applying if and ifelse functions

流过昼夜 提交于 2019-12-04 05:21:17

There's a lot that needs explaining here. Let's start with the simplest case. In your first example, all you need is:

mydf$z <- with(mydf,ifelse(x == 1,0,n))

An equivalent ddply solution might look like this:

ddply(mydf,.(x),transform,z = ifelse(x == 1,0,n))

Probably your biggest source of confusion is that you seem to not understand what is being passed as arguments to functions within ddply.

Consider your first attempt:

k <- function(x) {
  mydf$z <- ifelse(x == 1, 0, mydf$n)
  return (mydf)
}

The way ddply works is that it splits mydf up into several, smaller data frame, based on the values in the column x. That means that each time ddply calls k, the argument passed to k is a data frame. Specifically, a subset of you primary data frame.

So within k, x is a subset of mydf, with all the columns. You should not be trying to modify mydf from within k. Modify x, and then return the modified version. (If you must, but the options I displayed above are better.) So we might re-write your k like this:

k <- function(x) {
  x$z <- ifelse(x$x == 1, 0, x$n)
  return (x)
}

Note that you've created some confusing stuff by using x as both an argument to k and as the name of one of our columns.

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