简体   繁体   English

数据框根据条件对行进行分组

[英]Data frame grouping rows based on condition

For some this is an easy exercise for myself is a little bit tricky.对于某些人来说,这是一个简单的练习,对我自己来说有点棘手。 I would like to calculate the amount of time persons with the same id spend in time t.我想计算具有相同 ID 的人在时间 t 上花费的时间。 Basically I would like to know the total amount of time people spend together and alone based on gender.基本上,我想知道人们根据性别共度和独处的总时间。

Input输入

id     DMSex       t1  t2  t3  t4  t5  t6 
12       M         15  0   0   15  15  15
12       F         0   15  15  0   0   15  
13       F         15  0   15  0   0   0
13       M         15  15   0  15  0   15

Output:输出:

 id  Together  Male only  Female only
 12    15           45       15
 13    15           45       30

You can also solve it as follows:您也可以按如下方式解决:

df <- read.table(text = "id     DMSex       t1  t2  t3  t4  t5  t6 
12       M         15  0   0   15  15  15
12       F         0   15  15  0   0   15  
13       F         15  0   15  0   0   0
13       M         15  15   0  15  0   15", header = TRUE)

library(data.table)

setDT(df)[order(DMSex), {
  pos <- sapply(.SD, function(x) all(x > 0))
  comm <- sum(.SD[1, pos, with = FALSE])
  onlyFM <- rowSums(.SD[, !pos, with = FALSE])
  .(together = comm, males_only = onlyFM[2], females_only = onlyFM[1])
}, by = id, .SDcols = t1:t6]

#       id together males_only females_only
# 1:    12       15         45           30
# 2:    13       15         45           15

Create male and female matrices and then use the indicated computations.创建男性和女性矩阵,然后使用指定的计算。

ix <- -(1:2)
males <- as.matrix(subset(DF, DMSex == "M")[ix])
females <- as.matrix(subset(DF, DMSex == "F")[ix])

data.frame(id = unique(DF$id),
  together = rowSums(pmin(females, males)),
  males_only = rowSums(pmax(males - females, 0)),
  females_only = rowSums(pmax(females - males, 0)))

giving:给予:

  id together males_only females_only
2 12       15         45           30
3 13       15         45           15

Note笔记

Lines <- "id     DMSex       t1  t2  t3  t4  t5  t6 
12       M         15  0   0   15  15  15
12       F         0   15  15  0   0   15  
13       F         15  0   15  0   0   0
13       M         15  15   0  15  0   15"
DF <- read.table(text = Lines, header = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM