本文介绍了为什么这个 R dplyr getAges 在有序列表中失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑通过以下函数 getAges 通过 id 列表获取年龄列表.它在整个代码示例中失败,请参阅以下完整代码,方法是在给定的 id 列表上以错误的顺序返回年龄.代码 DF[DF$ID %in% ids,] 获取整个数据(DF),考虑 ids(DF$ID),id 列表中的前者 (- - %in% ids),并返回这些 id 的年龄 ([wantedIds]$Age).我不确定 - - %in% ids 部分,因为 R %in% 比较会在匹配时返回 id.

I am thinking to get a list of ages by a list of ids by the following function getAges.It fails on the whole code example, see the following complete code, by returning ages in wrong order on the given id list.The code DF[DF$ID %in% ids,] takes the whole data (DF), considers ids (DF$ID), the former in the list of ids (- - %in% ids), and returns age of those ids ([wantedIds]$Age).I am unsure about the part - - %in% ids because R %in% compares returns the id if there is a match.

getAges <- function(...)
{
   DF[DF$ID %in% ids,]$Age
}

getIDs 函数正确返回.整个代码示例

The function getIDs returns correctly.The whole code example

library('dplyr')
getIDs <- function(..., by = NULL){
    DF %>% filter_(...) %>% { if (!is.null(by))  arrange_(., by) else . } %>% .$ID
}
getAges <- function(...)
{
   DF[DF$ID %in% ids,]$Age
}

DF <- structure(list(ID = c(16265L, 16272L, 16273L, 16420L, 16483L,
16539L, 16773L, 16786L, 16795L, 17052L, 17453L, 18177L, 18184L,
19088L, 19090L, 19093L, 19140L, 19830L), Age = c(32L, 20L, 28L,
38L, 42L, 35L, 26L, 32L, 20L, 45L, 32L, 26L, 34L, 41L, 45L, 34L,
38L, 50L), Gender = structure(c(2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("female",
"male"), class = "factor")), .Names = c("ID", "Age", "Gender"
), class = "data.frame", row.names = c(NA, -18L))

ids <- getIDs(by = "desc(Age)")

ages <- getAges(ids) # TODO this fails

str(ids)
str(ages)
#  int [1:18] 19830 17052 19090 16483 19088 16420 19140 16539 18184 19093 ...
# int [1:18] 32 20 28 38 42 35 26 32 20 45 ... # TODO why here this order?

原始数据列表

#Original
#ID Age Gender
#16265  32  male
#16272  20  female
#16273  28  female
#16420  38  female
#16483  42  male
#16539  35  female
#16773  26  male
#16786  32  female
#16795  20  female
#17052  45  female
#17453  32  female
#18177  26  female
#18184  34  female
#19088  41  female
#19090  45  male
#19093  34  male
#19140  38  female
#19830  50  female

getAges的预期输出:与列表ids

R:3.3.2
操作系统:Debian 8.5

R: 3.3.2
OS: Debian 8.5

推荐答案

如果 getAges 的唯一目的是查找 ids 的年龄,那么试试

If the only purpose of getAges is to lookup the ages of ids then try

getAges <- function(...)
{
   DF[match(ids,DF$ID),"Age"]
}

这篇关于为什么这个 R dplyr getAges 在有序列表中失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:02