[R] detecting measurement of specific id in column in R

PIKAL Petr petr@pik@l @ending from prechez@@cz
Fri Nov 23 14:02:32 CET 2018


Hi

It could be done by ave function if you prepare chamber column to such task

library(zoo)
#make new column ch
rrdf$ch<-rrdf$chamber
#change what is not IN to NA
rrdf$ch[which(rrdf$chamber!="IN")]<-NA
#make distinct identifier for each IN chunk
rrdf$ch[which(rrdf$chamber=="IN")]<-paste("IN", 1:3, sep="")
fill NA values
rrdf$ch<-na.locf(rrdf$ch)

ave(rrdf[,4], rrdf$ch, FUN=function(x) x-x[1])

gives you values which could be added to original data e.g.

rrdf$ppm_13CO2_delta <- ave(rrdf$ ppm_13CO2, rrdf$ch, FUN=function(x) x-x[1])

Cheers
Petr

> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Jim Lemon
> Sent: Thursday, November 22, 2018 11:34 PM
> To: ro.rehschuh using googlemail.com
> Cc: r-help mailing list <r-help using r-project.org>
> Subject: Re: [R] detecting measurement of specific id in column in R
>
> Hi Vicci,
> It's very clunky, but I think it will do what you want.
> rrdf<-read.csv(text="No,date,chamber,d13C,ppm_CO2,ppm_13CO2
>  1,10.14.2018 10:43 PM,IN,-0.192,439.6908,4.9382
>  2,10.14.2018 10:47 PM,101,-0.058,440.7646,4.9509
>  3,10.14.2018 10:50 PM,103,-1.368,535.6602,5.9967
>  4,10.14.2018 10:53 PM,104,-1.601,542.4841,6.0702
>  5,10.14.2018 10:57 PM,105,-1.353,475.2809,5.3299
>  6,10.14.2018 11:00 PM,106,-1.184,530.6732,5.9430
>  7,10.14.2018 11:03 PM,107,-1.582,512.5939,5.7418
>  8,10.14.2018 11:07 PM,108,-1.359,544.0658,6.0889
>  9,10.14.2018 11:10 PM,109,-1.557,543.2651,6.0790
>  10,10.14.2018 11:13 PM,110,-1.638,477.0006,5.3476
>  11,10.14.2018 11:17 PM,111,-1.475,530.1569,5.9357
>  12,10.14.2018 11:20 PM,IN,-0.039,439.3367,4.9350
>  13,10.14.2018 11:23 PM,1,-0.061,439.7931,4.9400
>  14,10.14.2018 11:26 PM,3,-0.510,456.0714,5.1201
>  15,10.14.2018 11:30 PM,4,-0.510,456.5144,5.1250
>  16,10.14.2018 11:33 PM,5,-0.767,454.4449,5.1005
>  17,10.14.2018 11:37 PM,6,-0.788,459.7679,5.1600
>  18,10.14.2018 11:40 PM,7,-0.978,456.6323,5.1240
>  19,10.14.2018 11:43 PM,8,-0.742,450.4059,5.0556
>  20,10.14.2018 11:47 PM,9,-0.675,451.6678,5.0700
>  21,10.14.2018 11:50 PM,10,-0.880,455.5837,5.1127
>  22,10.14.2018 11:53 PM,11,-0.912,463.0478,5.1960
>  23,10.15.2018 12:01 AM,IN,-0.368,439.5525,4.9359
>  24,10.15.2018 12:12 AM,102,-0.205,439.9343,4.9409
>  25,10.15.2018 12:15 AM,112,-1.685,474.5002,5.3196
>  26,10.15.2018 12:19 AM,113,-1.714,474.4248,5.3186
>  27,10.15.2018 12:22 AM,114,-2.032,496.5623,5.5623
>  28,10.15.2018 12:26 AM,115,-1.602,471.2034,5.2834
>  29,10.15.2018 12:29 AM,116,-1.303,554.4268,6.2028
>  30,10.15.2018 12:32 AM,117,-1.833,501.2357,5.6151
>  31,10.15.2018 12:36 AM,118,-1.745,496.0126,5.5578
>  32,10.15.2018 12:39 AM,119,-1.537,467.5305,5.2428
>  33,10.15.2018 12:42 AM,120,-2.109,507.5778,5.6836",
>  stringsAsFactors=FALSE)
>
>
> rrdf$ppm_13CO2_delta<-rrdf$ppm_CO2_delta<-rrdf$d13C_delta<-NA
> for(row in 1:nrow(rrdf)) {
>  if(rrdf$chamber[row] == "IN")
>   INval<-c(rrdf$d13C[row],rrdf$ppm_CO2[row],rrdf$ppm_13CO2[row])
>  rrdf[row,c("d13C_delta","ppm_CO2_delta","ppm_13CO2_delta")]<-
>   rrdf[row,c("d13C","ppm_CO2","ppm_13CO2")]-INval
> }
>
> Jim
>
> On Fri, Nov 23, 2018 at 8:52 AM Romy Rehschuh via R-help <r-help using r-
> project.org> wrote:
> >
> > Dear all,
> >
> > if the attachment didn´t arrive, maybe it works now.
> > I would like to substract the "IN" values (= the air which goes into
> > the
> > chambers) for "d13C", "ppm_CO2" and "ppm_13CO2"
> > from the "d13C", "ppm_CO2" and "ppm_13CO2" for every single chamber.
> > I need to substract the "IN" values which were measured *before* the
> > chamber.
> >
> > So the calculation would look like df$d13C [chambers] - df$d13C [IN]
> >                                                        df$ppm_CO2
> > [chambers] - df$ppm_13CO2 [IN]
> >                                                        df$ppm_13CO2
> > [chambers] - df$ ppm_13CO2 [IN]
> > --> for chamber 101-111 this should be the first "IN" (No 1) for
> > --> chamber 1-11 this should be the second "IN" (No 12)
> > ...and so on
> >
> > I tried sth. like which(abs(date-x) == min(abs(date-x), but it just
> > gives me the closest "IN" in time and not the "IN" before.
> >
> > I would appreciate any help!
> > Thank you so much, Vicci
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/



More information about the R-help mailing list