简体   繁体   English

将ddply的当前值传递给函数

[英]Passing current value of ddply split on to function

Here is some sample data for which I want to encode the gender of the names over time: 这是一些示例数据,我想随着时间的推移对名称的性别进行编码:

names_to_encode <- structure(list(names = structure(c(2L, 2L, 1L, 1L, 3L, 3L), .Label = c("jane", "john", "madison"), class = "factor"), year = c(1890, 1990, 1890, 1990, 1890, 2012)), .Names = c("names", "year"), row.names = c(NA, -6L), class = "data.frame")

Here is a minimal set of the Social Security data, limited to just those names from 1890 and 1990: 这是社会保障数据的最小集合,仅限于1890年和1990年的那些名称:

ssa_demo <- structure(list(name = c("jane", "jane", "john", "john", "madison", "madison"), year = c(1890L, 1990L, 1890L, 1990L, 1890L, 1990L), female = c(372, 771, 56, 81, 0, 1407), male = c(0, 8, 8502, 29066, 14, 145)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), .Names = c("name", "year", "female", "male"))

I've defined a function which subsets the Social Security data given a year or range of years. 我定义了一个函数,该函数将给定一年或几年范围内的社会保障数据作为子集。 In other words, it calculates whether a name was male or female over a given time period by figuring out the proportion of male and female births with that name. 换句话说,它通过计算具有该名字的男女出生比例来计算在给定时间段内一个名字是男性还是女性。 Here is the function along with a helper function: 这是该函数以及一个辅助函数:

require(plyr)
require(dplyr)

select_ssa <- function(years) {

  # If we get only one year (1890) convert it to a range of years (1890-1890)
  if (length(years) == 1) years <- c(years, years)

  # Calculate the male and female proportions for the given range of years
  ssa_select <- ssa_demo %.%
    filter(year >= years[1], year <= years[2]) %.%
    group_by(name) %.%
    summarise(female = sum(female),
              male = sum(male)) %.%
    mutate(proportion_male = round((male / (male + female)), digits = 4),
           proportion_female = round((female / (male + female)), digits = 4)) %.%
    mutate(gender = sapply(proportion_female, male_or_female))

  return(ssa_select)
}

# Helper function to determine whether a name is male or female in a given year
male_or_female <- function(proportion_female) {
  if (proportion_female > 0.5) {
    return("female")
  } else if(proportion_female == 0.5000) {
    return("either")
  } else {
    return("male")
  }
}

Now what I want to do is use plyr, specifically ddply , to subset the data to be encoded by year, and merge each of those pieces with the value returned by the select_ssa function. 现在,我想做的是使用plyr,特别是ddply ,按年对要编码的数据进行子集化,然后将这些片段中的每一个与select_ssa函数返回的值合并。 This is the code I have. 这是我的代码。

ddply(names_to_encode, .(year), merge, y = select_ssa(year), by.x = "names", by.y = "name", all.x = TRUE)

When calling select_ssa(year) , this command works just fine if I hard code a value like 1890 as the argument to the function. 调用select_ssa(year) ,如果我将诸如1890之类的值硬编码为该函数的参数,则此命令可以正常工作。 But when I try to pass it the current value for year that ddply is working with, I get an error message: 但是,当我尝试将其传递给ddply使用的year的当前值时,我收到一条错误消息:

Error in filter_impl(.data, dots(...), environment()) : 
  (list) object cannot be coerced to type 'integer'

How can I pass the current value of year on to ddply ? 如何将year的当前值传递给ddply

I think you're making things too complicated by trying to do a join inside ddply . 我认为您尝试在ddply内进行ddply使事情变得太复杂了。 If I were to use dplyr I would probably do something more like this: 如果我要使用dplyr我可能会做更多这样的事情:

names_to_encode <- structure(list(name = structure(c(2L, 2L, 1L, 1L, 3L, 3L), .Label = c("jane", "john", "madison"), class = "factor"), year = c(1890, 1990, 1890, 1990, 1890, 2012)), .Names = c("name", "year"), row.names = c(NA, -6L), class = "data.frame")

ssa_demo <- structure(list(name = c("jane", "jane", "john", "john", "madison", "madison"), year = c(1890L, 1990L, 1890L, 1990L, 1890L, 1990L), female = c(372, 771, 56, 81, 0, 1407), male = c(0, 8, 8502, 29066, 14, 145)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), .Names = c("name", "year", "female", "male"))

names_to_encode$name <- as.character(names_to_encode$name)
names_to_encode$year <- as.integer(names_to_encode$year)

tmp <- left_join(ssa_demo,names_to_encode) %.%
        group_by(year,name) %.%
        summarise(female = sum(female),
              male = sum(male)) %.%
        mutate(proportion_male = round((male / (male + female)), digits = 4),
           proportion_female = round((female / (male + female)), digits = 4)) %.%
        mutate(gender = ifelse(proportion_female == 0.5,"either",
                        ifelse(proportion_female > 0.5,"female","male")))

Note that 0.1.1 is still a little finicky about the types of join columns, so I had to convert them. 请注意,0.1.1仍然对联接列的类型有些挑剔,因此我不得不对其进行转换。 I think I saw some activity on github that suggested that was either fixed in the dev version, or at least something they're working on. 我想我在github上看到了一些活动,这些活动建议要么在开发版本中已修复,要么至少是他们正在从事的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM