简体   繁体   English

根据另一列中的值减去一列中的数据

[英]Subtracting data in one column based on values in another column

I want to subtract the values in the Temp column based on the values in the sensor column. 我想基于传感器列中的值减去“温度”列中的值。 Grouped by each date and location, I want to subtract Temp with the sensor value of 1 from Temp with a sensor value of 2. See data sample below. 按每个日期和位置分组,我想从传感器值为2的Temp中减去传感器值为1的Temp。请参见下面的数据示例。

date <- c("2016-03-21","2016-03-21","2016-03-21","2016-03-21","2016-03-21","2016-03-21")
location <- c(1,1,2,2,3,3)
sensor <- c(1,16,1,16,1,16)
Temp <- c(35,34,45,42,46,47)
df <- data.frame(date,location,sensor,Temp)

This is my attempt at trying to do so using dplyr ... 这是我尝试使用dplyr这样做的尝试...

test <- df %>% group_by(date,location,sensor) %>% lfMaxTemp$Temp["sensor"==1]-lfMaxTemp$Temp["sensor"==16]

This is the result I would like: 这是我想要的结果:

        date location diff
1 2016-03-21        1    1
2 2016-03-21        2    3
3 2016-03-21        3   -1
library(dplyr)
df %>% group_by(date, location) %>% summarise(diff = Temp[sensor==1]- Temp[sensor==16])
#        date location  diff
#1 2016-03-21        1     1
#2 2016-03-21        2     3
#3 2016-03-21        3    -1

We can use data.table . 我们可以使用data.table Convert the 'data.frame' to 'data.table' ( setDT(df) ), grouped by 'date', 'location', order the rows based on 'sensor' in descending, and we summarise the output by the taking the difference between the 1st and 2nd observations with diff 转换“data.frame”到“data.table”( setDT(df)由“日期”,“位置”分组, order基于在下行“传感器”的行中,而我们通过取总结输出diff的第一和第二观察之间的diff

library(data.table)
setDT(df)[order(-sensor), .(Diff  = diff(Temp)), .(date, location)]
#         date location Diff
#1: 2016-03-21        1    1
#2: 2016-03-21        2    3
#3: 2016-03-21        3   -1

NOTE: Here we assume that there are only 1 and 16 values in 'Temp' for each 'Temp' 注意:这里我们假设每个“温度”在“温度”中只有1个值和16个值

In case, there are other values in 'Temp', just do a filter before the group by step 如果'Temp'中还有其他值,只需在分组之前先进行过滤

 setDT(df)[Temp %in% c(1, 16)][order(-sensor), .(Diff  = diff(Temp)), .(date, location)]

data 数据

df <- data.frame(date,location,sensor,Temp)

Comments 评论

It is not recommended to use as.data.frame(cbind(.. to construct the data.frame as it can result in all the columns to be in class factor/character . 不建议使用as.data.frame(cbind(..来构造data.frame因为它可能导致所有列都在class factor/character

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

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