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

问题描述

我想通过以下函数 getAges 获取ids列表的年龄列表。
在整个代码示例中失败,请参阅以下完整代码,通过在给定的ID列表中以错误的顺序返回年龄。
代码 DF [%ID中的DF $ ID%] 获取整个数据( DF ) ,考虑了ids( DF $ ID ),前者位于ids列表中( - - %ids 中的%) ,并返回这些ID的年龄( [wantedIds] $ Age )。
我不确定%$ / code中的 - - %,因为% compare中的R %返回如果有匹配,则为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

Expected output of getAges: list of ages corresponding to the order of the list 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在有序列表上失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:27