mardijk
2012-Nov-01 15:18 UTC
[R] Select the last rows of when specific conditions are met.
Hello, As I am new with R I am completely stuck in resolving a, no doubt, easy problem. I have a dataset with an enormous amount of rows and 17 columns. I need to know per trial and subject number if another variable (tt) exceeds a maximum. If this is true than the last 5000 rows of that specific trial for that subject number needs to be deleted. But I am completely stuck on how to do so. After various attempts I have finally come up with the following (which obviously does not work). if (dat["tt"]>6000 ){ return( tail(dattest[,c("Trial", "Subj" )],n=5000) <- NA } Can anybody explain what I need to do? Thank you so much! -- View this message in context: http://r.789695.n4.nabble.com/Select-the-last-rows-of-when-specific-conditions-are-met-tp4648133.html Sent from the R help mailing list archive at Nabble.com.
Jessica Streicher
2012-Nov-01 17:15 UTC
[R] Select the last rows of when specific conditions are met.
I really don't understand what you want to achieve and how your data presents itself. You have several combinations of trial/subject which are unique? There is a variable for each such combination that you want to test against (tt)? In addition you have data on each such combination (several sets with 17 columns ) ? or each column is a subject and each row a trial or something along those lines? On 01.11.2012, at 16:18, mardijk wrote:> Hello, > > As I am new with R I am completely stuck in resolving a, no doubt, easy > problem. > > I have a dataset with an enormous amount of rows and 17 columns. I need to > know per trial and subject number if another variable (tt) exceeds a > maximum. If this is true than the last 5000 rows of that specific trial for > that subject number needs to be deleted. > > But I am completely stuck on how to do so. After various attempts I have > finally come up with the following (which obviously does not work). > > > if (dat["tt"]>6000 > ){ > return( > tail(dattest[,c("Trial", "Subj" )],n=5000) <- NA > } > > Can anybody explain what I need to do? > > Thank you so much! > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Select-the-last-rows-of-when-specific-conditions-are-met-tp4648133.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.
mardijk
2012-Nov-01 19:27 UTC
[R] Select the last rows of when specific conditions are met.
I am so sorry for the lack of clarity. I have a few subjects, every subject has done a number of trials and per trial there is a measurement per millisecond (represented in the variable tt), so every unique trial is represented in around 2500 rows. An example of data would be: Subj | Trial | tt | v4 | v5 | ... | v17 1 1 1 .. 1 1 2 .. 1 1 3 .. 1 1 4 .. 1 1 5 .. 1 2 1 .. 1 2 2 .. 1 2 3 .. 1 3 1 .. 1 3 2 .. 1 3 3 .. 2 1 1 .. 2 1 2 .. 2 1 3 .. etc. In the above dataset, how can I single out the trial that > 4, and delete the last two rows (tt = 4&5). I hope this makes more sense. -- View this message in context: http://r.789695.n4.nabble.com/Select-the-last-rows-of-when-specific-conditions-are-met-tp4648133p4648157.html Sent from the R help mailing list archive at Nabble.com.
HI, Saw your response in Nabble. Try this: dat1<-read.table(text=" Subj Trial? tt? 1????????? 1????? 1??? 1????????? 1????? 2??? 1????????? 1????? 3??? 1????????? 1????? 4???? 1????????? 1????? 5???? 1????????? 2????? 1???? 1????????? 2????? 2???? 1????????? 2????? 3???? 1????????? 3????? 1???? 1????????? 3????? 2???? 1????????? 3????? 3???? 2????????? 1????? 1???? 2????????? 1????? 2???? 2????????? 1????? 3 ",sep="",header=TRUE)? ?? res<-do.call(rbind,lapply(split(dat1,list(dat1$Subj,dat1$Trial)),function(x) x[x[,3]<4,])) ?rownames(res)<-1:nrow(res) ?res #?? Subj Trial tt #1???? 1???? 1? 1 #2???? 1???? 1? 2 #3???? 1???? 1? 3 #4???? 2???? 1? 1 #5???? 2???? 1? 2 #6???? 2???? 1? 3 #7???? 1???? 2? 1 #8???? 1???? 2? 2 #9???? 1???? 2? 3 #10??? 1???? 3? 1 #11??? 1???? 3? 2 #12??? 1???? 3? 3 ?A.K. ----- Original Message ----- From: mardijk <van_dijkmarianne at hotmail.com> To: r-help at r-project.org Cc: Sent: Thursday, November 1, 2012 11:18 AM Subject: [R] Select the last rows of when specific conditions are met. Hello, As I am new with R I am completely stuck in resolving a, no doubt, easy problem. I have a dataset with an enormous amount of rows and 17 columns. I need to know per trial and subject number if another variable (tt) exceeds a maximum. If this is true than the last 5000 rows of that specific trial for that subject number needs to be deleted. But I am completely stuck on how to do so. After various attempts I have finally come up with the following (which obviously does not work). if (dat["tt"]>6000 ){ ??? return( ??? tail(dattest[,c("Trial", "Subj" )],n=5000) <- NA } Can anybody explain what I need to do? Thank you so much! -- View this message in context: http://r.789695.n4.nabble.com/Select-the-last-rows-of-when-specific-conditions-are-met-tp4648133.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.
HI, Instead of splitting, you could also do this: dat1<-read.table(text=" Subj Trial? tt? 1????????? 1????? 1??? 1????????? 1????? 2??? 1????????? 1????? 3??? 1????????? 1????? 4???? 1????????? 1????? 5???? 1????????? 2????? 1???? 1????????? 2????? 2???? 1????????? 2????? 3???? 1????????? 3????? 1???? 1????????? 3????? 2???? 1????????? 3????? 3???? 2????????? 1????? 1???? 2????????? 1????? 2???? 2????????? 1????? 3 ",sep="",header=TRUE)? dat2<-dat1[as.logical(ave(dat1$tt,list(dat1$Subj,dat1$Trial),FUN=function(x) x<4)),] ?dat2 #?? Subj Trial tt #1???? 1???? 1? 1 #2???? 1???? 1? 2 #3???? 1???? 1? 3 #6???? 1???? 2? 1 #7???? 1???? 2? 2 #8???? 1???? 2? 3 #9???? 1???? 3? 1 #10??? 1???? 3? 2 #11??? 1???? 3? 3 #12??? 2???? 1? 1 #13??? 2???? 1? 2 #14??? 2???? 1? 3 A.K. ----- Original Message ----- From: mardijk <van_dijkmarianne at hotmail.com> To: r-help at r-project.org Cc: Sent: Thursday, November 1, 2012 11:18 AM Subject: [R] Select the last rows of when specific conditions are met. Hello, As I am new with R I am completely stuck in resolving a, no doubt, easy problem. I have a dataset with an enormous amount of rows and 17 columns. I need to know per trial and subject number if another variable (tt) exceeds a maximum. If this is true than the last 5000 rows of that specific trial for that subject number needs to be deleted. But I am completely stuck on how to do so. After various attempts I have finally come up with the following (which obviously does not work). if (dat["tt"]>6000 ){ ??? return( ??? tail(dattest[,c("Trial", "Subj" )],n=5000) <- NA } Can anybody explain what I need to do? Thank you so much! -- View this message in context: http://r.789695.n4.nabble.com/Select-the-last-rows-of-when-specific-conditions-are-met-tp4648133.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.