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L, 0L, 0L, 0L, 2L, 2L, 774L, 0L, 0L, 0L, 2L, 0L, 2L,
0L, 2L, 0L, 2L, 0L, 0L, 2L, 0L, 2L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
0L, 2L, 2L, 0L, 2L, 0L, 2L, 2L, 0L, 2L, 0L, 0L, 2L, 0L, 0L, 2L,
2L, 2L, 0L, 2L, 0L, 2L, 0L, 0L, 2L, 2L, 0L, 2L, 0L, 0L, 0L, 2L,
2L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
2L, 0L, 0L, 2L, 2L, 0L, 774L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L,
2L, 0L, 2L, 774L, 0L, 2L, 0L, 0L, 2L, 2L, 2L, 2L, 774L, 0L, 0L,
774L)), row.names = c(NA, -175L), class = c("tbl_df", "tbl",
"data.frame"))
我创建了一个函数,该函数应基于应按行应用于数据帧的嵌套条件,返回与nrow(df_headache_tibble)相同长度的字符向量(Q10_incidence)。Q10_incidence [i]应该是将该函数应用于df_headache_tibble [i,1]和df_headache_tibble [i,2]的结果,我打算为此使用mapply。
incidence_headaches<-function(x,y){
if (is.na(x)|is.na(y)){
output<-NA
}
else if (x==2){
if (y==2){
output<-'previous_headache_maintained'
}else if(y==0){
output<-'previous_headache_ceased'
}
}else if(x %in% c(0,774,775,776)){
if (y==2){
output<-'new_onset_headache'
}else if (y %in% c(0, 774, 775, 776)){
output<-'no_headache'
}
}
}
Q10_incidence<-mapply(incidence_headaches, Q10_headache_tibble[,1], Q10_headache_tibble[,2])
当我打电话
mapply(incidence_headaches, Q10_headache_tibble[,1], Q10_headache_tibble[,2])
在几个警告中,我得到了可怕的“条件的长度> 1,并且仅将使用第一个元素”。我该如何处理?尽管我发现了几个关于相同“条件有长度(...)”警告的问题,但我仍然觉得这个话题很混乱。欢迎进行“假人”演练。
它似乎与向量化有关,可以通过用嵌套的ifelse()结构代替该函数来解决,这可能会很混乱。
在很多情况下,我可能需要使用类似的功能,但不确定最好的解决方法是什么。
1)就我个人而言,我尝试仅使用其许多命令中的一小部分来尽可能多地执行R。也许一个简单apply
的方法可以更轻松地进行管理。apply
与MARGIN = 1
将给data.frame的每一行赋予一个函数。因此,我对您的功能进行了一些细微的更改(仅对前三行感兴趣,其余为复制和粘贴):
incidence_headaches<-function(row){
x <- row[1]
y <- row[2]
if (is.na(x)|is.na(y)){
output<-NA
}
else if (x==2){
if (y==2){
output<-'previous_headache_maintained'
}else if(y==0){
output<-'previous_headache_ceased'
}
}else if(x %in% c(0,774,775,776)){
if (y==2){
output<-'new_onset_headache'
}else if (y %in% c(0, 774, 775, 776)){
output<-'no_headache'
}
}
}
然后,您可以使用以下简单方法apply
:
apply(df_headache_tibble, MARGIN = 1, incidence_headaches)
要获得这样的东西:
> apply(df_headache_tibble, MARGIN = 1, incidence_headaches)
[1] "no_headache" "previous_headache_ceased" "previous_headache_maintained"
[4] "previous_headache_maintained" "new_onset_headache" "no_headache"
[7] "no_headache" "no_headache" "previous_headache_ceased"
[10] "new_onset_headache" "previous_headache_ceased" "previous_headache_maintained"
[13] "no_headache" "previous_headache_ceased" "no_headache"
...
2) mapply
显然是一个完美的工作功能,没有理由不使用它。您的问题是:小动作是data.frames,但它们的行为不像data.frames。这很好用:
mapply(incidence_headaches,
as.data.frame(df_headache_tibble)[,1],
as.data.frame(df_headache_tibble)[,2])
当您只对data.frame中的一行进行子集处理时,它将为您提供矢量;当您只对某个小标题进行子集处理时,将为您提供一个小标题。与发明R data.frame的人相比,Hadley对事物应如何运作有不同的看法。有一些解决方法,例如
mapply(incidence_headaches,
df_headache_tibble[,1, drop = TRUE],
df_headache_tibble[,2, drop = TRUE])
请在此处阅读详细信息,但通常要始终注意,尽管小标题是data.frames,但它们的行为并不完全像data.frames:https://tibble.tidyverse.org/reference/subsetting.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句