首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从分组数据的最后一项中减去第一项

如何从分组数据的最后一项中减去第一项
EN

Stack Overflow用户
提问于 2014-05-13 14:21:13
回答 2查看 3.8K关注 0票数 7

我希望在以下任务中提供一些帮助:从下面的数据框架(C)中,对于每个id,我想从最后一个条目中减去列d_2下的第一个条目,然后将结果存储在包含相同id的另一个数据帧中。然后,我可以将它与我的初始数据合并。请注意,减法必须按此顺序进行(最后一项减去每个id的第一项)。

以下是密码:

代码语言:javascript
复制
id <- c("A1", "A1", "B10","B10", "B500", "B500", "C100", "C100", "C100", "D40", "D40", "G100", "G100")

d_1 <- c( rep(1.15, 2), rep(1.44, 2), rep(1.34, 2), rep(1.50, 3), rep(1.90, 2), rep(1.59, 2))

set.seed(2)

d_2 <- round(runif(13, -1, 1), 2)

C <- data.frame(id, d_1, d_2)

id   d_1   d_2
A1   1.15 -0.63
A1   1.15  0.40
B10  1.44  0.15
B10  1.44 -0.66
B500 1.34  0.89
B500 1.34  0.89
C100 1.50 -0.74
C100 1.50  0.67
C100 1.50 -0.06
D40  1.90  0.10
D40  1.90  0.11
G100 1.59 -0.52
G100 1.59  0.52

预期结果:

代码语言:javascript
复制
id2 <- c("A1", "B10", "B500", "C100", "D40", "G100")

difference <- c(1.03, -0.81, 0, 0.68, 0.01, 1.04)

diff_df <- data.frame(id2, difference)

id2    difference
A1        1.03
B10      -0.81
B500      0.00
C100      0.68
D40       0.01
G100      1.04

我尝试使用ddply来获取第一个和最后一个条目,但是我真的很难为第二个代码(下面)中的“函数参数”建立索引,以获得想要的结果。

代码语言:javascript
复制
C_1 <- ddply(C, .(id), function(x) x[c(1, nrow(x)), ])

ddply(C_1, .(patient), function )

老实说,我不太熟悉这个蹒跚学步的包--我从堆栈交换上的另一个post获得了上面的代码。

我的原始数据是一个groupedData,我相信另一种处理方法是使用gapply,但是我仍然在努力处理第三个参数(通常是一个函数)。

代码语言:javascript
复制
grouped_C <- groupedData(d_1 ~ d_2 | id, data = C, FUN = mean, labels = list( x = "", y = ""), units = list(""))

x1 <- gapply(grouped_C, "d_2", first_entry)

x2 <- gapply(grouped_C, "d_2", last_entry)

其中first_entry和last_entry是帮助我获得第一个和最后一个条目的函数。然后,我可以得到与:x2 - x1的区别。但是,我不知道在上面的代码中以first_entry和last_entry的形式输入什么(可能与头或尾有关)。

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-13 14:47:05

使用dplyr可以很容易地做到这一点。lastfirst函数对此任务非常有用。

代码语言:javascript
复制
library(dplyr)               #install the package dplyr and load it into library 

diff_df <- C %>%             #create a new data.frame (diff_df) and store the output of the following operation in it. The %.% operator is used to chain several operations together but you dont have to reference the data.frame you are using each time. so here we are using your data.frame C for the following steps
  group_by(id) %>%            #group the whole data.frame C by id
  summarize(difference = last(d_2)-first(d_2))     #for each group of id, create a single line summary where the first entry of d_2 (for that group) is subtracted from the last entry of d_2 for that group

#    id difference             #this is the result stored in diff_df
#1   A1       1.03
#2  B10      -0.81
#3 B500       0.00
#4 C100       0.68
#5  D40       0.01
#6 G100       1.04

编辑注意:使用%>%更新后的帖子,而不是不推荐的%.%

票数 14
EN

Stack Overflow用户

发布于 2017-09-15 21:59:25

如果你有任何单身汉,而他们需要单独呆着,那么这将解决你的问题。这与docendo弟子but的回答相同,但是使用if-else组件来处理单例案例:

代码语言:javascript
复制
library(dplyr)               
diff_df <- C %>%             
   group_by(id) %>%
   summarize(difference = if(n() > 1) last(d_2) - first(d_2) else d_2)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23633890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档