HCAI
HCAI

Reputation: 2263

How to add a specific value to all data by group?

I have a df of CO2 levels from three different sensors (A1,A2 and A3) but each sensor has a different offset. I would like to subtract the known offset (in a separate vector called offSet) from the data and add 400 to all data points in df$CO2.ppm.

df<-as.data.frame(Sensor=rep(c("A1","A2","A3"),each=50), CO2.ppm.=sample(400:1000, 150, replace=T))

offSets<-as.data.frame(Sensor=c("A1","A2","A3"),value=c(5,6,10))

Something like this makes sense in my mind but I can't quite get the syntax of the last bit right. Any help would be much appreciated, thank you.

df %>% 
  group_by(Sensor) %>% 
  mutate(CO2.ppm.OffSet=CO2.ppm.+400-rep(offSets$value,each=offSets) %>%
  group_by(Sensor) %>% 
  summarise(n()))

Upvotes: 3

Views: 84

Answers (2)

akrun
akrun

Reputation: 887168

We can use a data.table join for this

library(data.table)
setDT(df)[offSets, new_col := CO2.ppm + 400 - value, on = .(Sensor)][]
head(df)

data

set.seed(1234)
df<-data.frame(Sensor=rep(c("A1","A2","A3"),each=50), 
               CO2.ppm =sample(400:1000, 150, replace=T))
offSets<-data.frame(Sensor=c("A1","A2","A3"),value=c(5,6,10))

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

One way using dplyr could be to left_join on Sensor and perform the calculation

library(dplyr)
left_join(df, offSets, by = "Sensor") %>%
   mutate(new_col = CO2.ppm + 400 - value)

#    Sensor CO2.ppm value new_col
#1       A1     468     5     863
#2       A1     774     5    1169
#3       A1     766     5    1161
#4       A1     774     5    1169
#5       A1     917     5    1312
#6       A1     784     5    1179
#....

and in base R that would be

transform(merge(df, offSets, by = "Sensor", all.x = TRUE), 
          new_col = CO2.ppm + 400 - value)

We can also use match

df$new_col <- df$CO2.ppm + 400 - offSets$value[match(df$Sensor, offSets$Sensor)]

data

set.seed(1234)
df<-data.frame(Sensor=rep(c("A1","A2","A3"),each=50), 
               CO2.ppm =sample(400:1000, 150, replace=T))
offSets<-data.frame(Sensor=c("A1","A2","A3"),value=c(5,6,10))

Upvotes: 1

Related Questions