1

我正在尝试计算每人独特水果的平均数量(我通常的实践数据)。这对这两行代码都非常有效:

with(df, tapply(fruit, names, FUN = function(x) length(unique(x))))->uniques
sum(uniques)/length(unique(df$names))

aggregate(df[,"fruit"], by=list(id=names), FUN = function(x) length(unique(x)))->d1
sum(d1$x)/length(unique(df$names))

我的问题是,当我在真实数据上使用代码时,它不起作用。我的真实数据是处方数据,我想要平均每人独特药物的数量。使用 tapply 代码,它似乎创建了原始 df 中不存在的全新患者 ID。它还返回了 1000 个 NA 值。我的 id 列中没有缺失值,drug_code 列中也没有缺失值

with(dt3, tapply(drug_code, id, FUN = function(x) length(unique(x))))->uniques    

head(uniques)
                   uniques
Patient HAI0000001      NA
Patient HAI0000003      NA
Patient HAI0000008      NA
Patient HAI0000010      NA
Patient HAI0000014      NA
Patient HAI0000020      NA

table(dt3$id=="Patient HAI0000001")  ##checking to see if HA10000001 occurs in original df. the dim of df are 228954 rows and 5 cols

FALSE 
228954

对于聚合代码,我收到一个错误:

aggregate(dt3[,"drug_code"], by=list(id=id), FUN = function(x) length(unique(x)))->d1

Error in aggregate.data.frame(as.data.frame(x), ...) : 
  arguments must have same length

我不明白发生了什么。我的真实数据与我的实践数据相似,因为它有一个 id col 并有一个 drug/fruit 列。两个df中都没有丢失数据。我知道 lapply 更适合数据帧,但我不一定需要 df 返回。在任何情况下,tapply 代码都适用于 df 的练习数据。有谁知道这里发生了什么?

练习 DF:

 names<-as.character(c("john", "john", "john", "john", "john", "mary", "mary","mary","mary","mary", "jim", "sylvia","ted","ted","mary", "sylvia", "jim", "ted", "john", "ted"))
dates<-as.Date(c("2010-07-01",  "2010-09-01", "2010-11-01", "2010-12-01", "2011-01-01", "2010-08-12",  "2010-11-11", "2010-05-12",  "2010-12-03", "2010-07-12",  "2010-12-21", "2010-02-18",  "2010-10-29", "2010-08-13",  "2010-11-11", "2010-05-12",  "2010-04-01", "2010-05-06",  "2010-09-28", "2010-11-28" ))
fruit<-as.character(c("kiwi","apple","banana","orange","apple","orange","apple","orange", "apple", "apple", "pineapple", "peach", "nectarine", "grape", "melon", "apricot", "plum", "lychee", "watermelon", "apple" ))
df<-data.frame(names,dates,fruit) 

真实数据示例:

head(dt3)
        id         quantity   date_of_claim drug_code  index
1  Patient HAI0000560        1    2009-10-15 R03AC02 2010-04-06
2  Patient HAI0000560        1    2009-10-15 R03AK06 2010-04-06
3  Patient HAI0000560       30    2009-10-15 R03BB04 2010-04-06
4  Patient HAI0000560       30    2009-10-15 A02BC01 2010-04-06
5  Patient HAI0000560       50    2009-10-15 M02AA15 2010-04-06
6  Patient HAI0000560       30    2009-10-15 N02BE51 2010-04-06
4

2 回答 2

3

在您的情况下,您要询问一个数字:(unique(fruits))患者 ID 中特定向量的所有单独长度的平均值。这首先向您展示了个人唯一计数,然后是平均函数结果:

> with(df,  tapply(fruit, names, function(x) length(unique(x)) ))
   jim   john   mary sylvia    ted 
     2      5      3      2      4 
> mean ( with(df,  tapply(fruit, names, function(x) length(unique(x)) )) )
[1] 3.2

我会评论说,您在上面的代码中包含特定值的测试有一个尾随空格,这可能会导致问题。"string "不会相等"string"。我在我的 .Rprofile 文件中放了一份使用 trim 功能的副本,pkg::gdata以便我更容易处理这种可能性。

于 2013-07-18T16:40:56.427 回答
1

我可能会遗漏一些东西,但这里不是一个简单的tapply工作吗?下面的行计算了每人不同水果的数量

x=tapply(df$fruit,df$names,function(x){length(unique(x))})

然后mean(x)会给你所有人的平均值吗?

于 2013-07-18T16:40:57.817 回答