【发布时间】:2020-02-05 05:06:51
【问题描述】:
我正在尝试生成一个变量来标记家庭名册中受访者与其父母之间难以置信的年龄差异。 mapply() 在“二元运算符的非数字参数”上给出错误,而当我仅在两列上应用该函数时,我没有收到此错误。很感谢任何形式的帮助。下面,我试图做一个可重现的例子。
# Variables
respbirth <- c(1974, 1950, 1990, 1980 )
B1010_1 <- c(1950, 1960, 1960, 1979 )
B1040_1 <- c(3,3,3,3)
B1010_2 <- c(1974, NA, NA, 1975 )
B1040_2 <- c(3,1,3,3)
# Data frame
df <- data.frame(respbirth, B1010_1, B1040_1, B1010_2, B1040_2 )
df
# Generate empty variable for flaging cases
df$flag_parent <- FALSE
## Generate a function flagging implausible differences using year of birth
attach(df) # the function doesnt work without this for some reason
imp.parent <- function(data=df,parentAge=B1010_1,relationship=B1040_1) {
df$flag_parent <- with(df, ((respbirth-parentAge)<18) & (relationship==3))
return(df)
}
# Test
df <- imp.parent(parentAge=B1010_1,relationship=B1040_1)
# Apply this function to all columns
parentAge <- c(paste0("B1010_",1:19, sep=""))
relationship <- c(paste0("B1040_",1:19, sep=""))
mapply(imp.parent, parentAge, relationship )
【问题讨论】:
-
这里为什么有
library(tidyverse)?这里的一切都是基础。 -
imp.parent没有保存任何更改,它正在返回一个数字向量。如果您打算让它在框架中添加flag_parent然后返回框架,您需要这样做(例如,df或return(df)作为函数的最后一行) . -
您最初将
logical值分配给flag_parent,但随后在函数中分配numeric。然后你抱怨没有分配TRUE。我真的很困惑。 -
感谢@r2evans,第一个问题通过将函数分配给函数外的 df 得到解决——函数内的 return(df) 不起作用。我现在删除了 as.numeric() ,这是我在试验时添加的。但实际上无论哪种方式都有效,mapply() 出现同样的错误。
-
奇怪,你的函数的编写方式,这不应该工作:
df应该被一个向量覆盖,丢失所有的帧。imp.parent的返回值应该是其主体中最后一个表达式的返回值,在本例中为with(df, (...))。即使该值被存储为df$中的列(预先存在的或新的)并不重要,<-右侧的值是一个向量,这是函数调用返回的值。或者也许我错过了一些东西。 (你之前是attaching 它,由于这样的原因,这通常是不好的。)