Discussion:
[R] detecting measurement of specific id in column in R
Jeff Newmiller
2018-11-22 16:53:09 UTC
Permalink
You hope... but no... not really. You need to enhance your question with a (minimal) example of the data just after importing into R, and another example of what you think the result should look like. Also, you should provide your current best guess at what a calculation would include expressed in R to help clarify your description. In your case you might be able to separate your problem into "find a row number for the first reading" and then what calculations would make if you knew "n" was that row number... but those are guesses because I don't know what your data look like.

A recommended technique to provide data is to use the dput function to format R data and paste it into your email... read more at [1][2][3].

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html (read the vignette)
Dear all,
I hope this is the right way to ask questions.
I have a problem with R regarding the detection of the measurement of a
specific sample_id (see example file attached). I have to substract the
"IN" values (means the air which goes into the chambers) from the
values of
"d13C", "ppm_CO2" and "ppm_13CO2" for every single chamber (=sample
ID).
The "IN" values have to be the ones which were measured* before *the
measurements of the single chambers in time. I measured "IN" once and
then
up to 10 chambers in a row to safe time, then "IN" again, but it can
change.
Therefore, searching for the closest "IN" does not work.
Do you have any suggestions? Would it be possible to write a loop for
this?
I would really much appreciate your help!
Best, Vicci
______________________________________________
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.
--
Sent from my phone. Please excuse my brevity.

______________________________________________
R-***@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.
Rui Barradas
2018-11-22 16:53:51 UTC
Permalink
Hello,

No attachment arrived.
R-help allows only a limited number of file types to be attached, if you
file is a text file, change its extension to .txt an try again, please.

Hope this helps,

Rui Barradas
Dear all,
I hope this is the right way to ask questions.
I have a problem with R regarding the detection of the measurement of a
specific sample_id (see example file attached). I have to substract the
"IN" values (means the air which goes into the chambers) from the values of
"d13C", "ppm_CO2" and "ppm_13CO2" for every single chamber (=sample ID).
The "IN" values have to be the ones which were measured* before *the
measurements of the single chambers in time. I measured "IN" once and then
up to 10 chambers in a row to safe time, then "IN" again, but it can change.
Therefore, searching for the closest "IN" does not work.
Do you have any suggestions? Would it be possible to write a loop for this?
I would really much appreciate your help!
Best, Vicci
______________________________________________
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.
______________________________________________
R-***@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.
Jim Lemon
2018-11-22 22:34:17 UTC
Permalink
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
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-***@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.
PIKAL Petr
2018-11-23 13:02:32 UTC
Permalink
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-----
Sent: Thursday, November 22, 2018 11:34 PM
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
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
______________________________________________
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/

______________________________________________
R-***@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

Bert Gunter
2018-11-22 18:06:24 UTC
Permalink
Jeff's advice is sound, but as I have a bit of time on my hand, I'll take a
guess at it. If it's wrong, then follow Jeff's advice so that we don't have
to continue to guess -- and do as he describes in any future posts, of
course. Note also that the mail server strips off attachments (except for a
few special types -- see the mailing list instructions for which), so you
need to follow the posting guide to post example data (see ?dput) .

It sounds to me as if your data are in a data frame, d, that looks like
this:

Sample_ID IN d13c ppm_CO2 ppm_13CO2 .......
1 v1 x1 y1 z1
1 v2 x2 y2 z2
. ........... ..................................
. ...................................................
1
2 vm xm ym zm
.
.
2 etc.
3
.
.

If so, then something like

nm <- c("d13c", "ppm_CO2", "ppm_13CO2")
by(d, d$Sample_ID, function(x){ x[, nm] - x[1, "IN"] }, simplify = FALSE )

would do what you seem to request. See ?by and associated links for details.

Again, if this is not what you want, do not ask me for further help. I'm
done guessing.

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Nov 22, 2018 at 8:14 AM Romy Rehschuh via R-help <
Dear all,
I hope this is the right way to ask questions.
I have a problem with R regarding the detection of the measurement of a
specific sample_id (see example file attached). I have to substract the
"IN" values (means the air which goes into the chambers) from the values of
"d13C", "ppm_CO2" and "ppm_13CO2" for every single chamber (=sample ID).
The "IN" values have to be the ones which were measured* before *the
measurements of the single chambers in time. I measured "IN" once and then
up to 10 chambers in a row to safe time, then "IN" again, but it can
change.
Therefore, searching for the closest "IN" does not work.
Do you have any suggestions? Would it be possible to write a loop for this?
I would really much appreciate your help!
Best, Vicci
______________________________________________
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.
[[alternative HTML version deleted]]

______________________________________________
R-***@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.
Loading...