Hi, I need to subset different levels of vector in a dataset to create a new dataframe that contains only these. These observations are not numerical, so I can't use the subset() function (at least this is the response I get from R). Suppose the dataframe looks like this: ParticipID ERP Electrode 1 s1 0.0370 FP1 2 s2 35.0654 FP2 3 s3 -3.3852 F4 4 s4 2.6119 P3 5 s5 0.1224 P4 6 s6 -5.3153 O1 I want to create a subset of the dataframe with only the FP1, FP2, F4 levels of Electrode, how do I do? (actually the levels I have to filter are much more than these three, but this is just to give an example). I know this is a trivial question, but I am still getting familiar with R... Thanks a lot, s. [[alternative HTML version deleted]]
On Aug 10, 2012, at 4:07 AM, simona mancini <mancinisimona at yahoo.it> wrote:> Hi, > > > I need to subset different levels of vector in a dataset to create a new dataframe that contains only these. These observations are not numerical, so I can't use the subset() function (at least this is the response I get from R). > Suppose the dataframe looks like this: > > > > ParticipID ERP Electrode > 1 s1 0.0370 FP1 > 2 s2 35.0654 FP2 > 3 s3 -3.3852 F4 > 4 s4 2.6119 P3 > 5 s5 0.1224 P4 > 6 s6 -5.3153 O1 > > > I want to create a subset of the dataframe with only the FP1, FP2, F4 levels of Electrode, how do I do? > (actually the levels I have to filter are much more than these three, but this is just to give an example). > > I know this is a trivial question, but I am still getting familiar with R... > > Thanks a lot, > > s.Take a look at ?subset and ?"%in%, which can be used in this fashion: NewDF <- subset(DF, Electrode %in% c("FP1", "FP2", "F4")) If the levels you do not want are few in number, it may be easier to use: NewDF <- subset(DF, !Electrode %in% c("Levels", "You", "Don't", "Want")) Note the use of '!' before Electrode to negate the boolean logic. Regards, Marc Schwartz
Hello, I don't see the problem. d <- read.table(text=" ParticipID ERP Electrode 1 s1 0.0370 FP1 2 s2 35.0654 FP2 3 s3 -3.3852 F4 4 s4 2.6119 P3 5 s5 0.1224 P4 6 s6 -5.3153 O1 ", header=TRUE) str(d) wanted <- c("FP1", "FP2", "F4") subset(d, Electrode %in% wanted) #output # ParticipID ERP Electrode #1 s1 0.0370 FP1 #2 s2 35.0654 FP2 #3 s3 -3.3852 F4 But if you can't subset because the variable Electrode is a factor, use as.character(Electrode). Hope this helps, Rui Barradas Em 10-08-2012 10:07, simona mancini escreveu:> Hi, > > > I need to subset different levels of vector in a dataset to create a new dataframe that contains only these. These observations are not numerical, so I can't use the subset() function (at least this is the response I get from R). > Suppose the dataframe looks like this: > > > > ParticipID ERP Electrode > 1 s1 0.0370 FP1 > 2 s2 35.0654 FP2 > 3 s3 -3.3852 F4 > 4 s4 2.6119 P3 > 5 s5 0.1224 P4 > 6 s6 -5.3153 O1 > > > I want to create a subset of the dataframe with only the FP1, FP2, F4 levels of Electrode, how do I do? > (actually the levels I have to filter are much more than these three, but this is just to give an example). > > I know this is a trivial question, but I am still getting familiar with R... > > Thanks a lot, > > s. > > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help@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.[[alternative HTML version deleted]]
subset should work fine. My guess would be that Electrode is a character or factor variable. Use str() to see what kind of variables you have in the data set. If we call the data set dat1 this works. subset(dat1, dat1$Electrode =="FP1" | dat1$Electrode =="FP2" | dat1$Electrode == "F4") John Kane Kingston ON Canada> -----Original Message----- > From: mancinisimona at yahoo.it > Sent: Fri, 10 Aug 2012 10:07:31 +0100 (BST) > To: r-help at r-project.org > Subject: [R] subsetting levels of a vector > > Hi, > > > I need to subset different levels of vector in a dataset to create a new > dataframe that contains only these. These observations are not numerical, > so I can't use the subset() function (at least this is the response I get > from R). > Suppose the dataframe looks like this: > > > > ParticipID ERP Electrode > 1 s1 0.0370 FP1 > 2 s2 35.0654 FP2 > 3 s3 -3.3852 F4 > 4 s4 2.6119 P3 > 5 s5 0.1224 P4 > 6 s6 -5.3153 O1 > > > I want to create a subset of the dataframe with only the FP1, FP2, F4 > levels of Electrode, how do I do? > (actually the levels I have to filter are much more than these three, but > this is just to give an example). > > I know this is a trivial question, but I am still getting familiar with > R... > > Thanks a lot, > > s. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
Hi, Try this: dat <- read.table(text=" ParticipID??? ERP? Electrode 1??????? s1? 0.0370????? FP1 2??????? s2 35.0654????? FP2 3??????? s3 -3.3852??????? F4 4??????? s4? 2.6119??????? P3 5??????? s5? 0.1224??????? P4 6??????? s6 -5.3153??????? O1 7??????? s7 -3.88????????? F4 8??????? s8? -4.988??????? FP1? ", header=TRUE) str(d) tomatch <- c("FP1", "FP2", "F4") #You can use either one of these dat[with(dat,Electrode%in%tomatch),] dat[dat$Electrode%in%tomatch,] #or dat2<-within(dat,{Electrode=Electrode%in%tomatch}) dat[dat2$Electrode==TRUE,] #? ParticipID???? ERP Electrode #1???????? s1? 0.0370?????? FP1 #2???????? s2 35.0654?????? FP2 #3???????? s3 -3.3852??????? F4 #7???????? s7 -3.8800??????? F4 #8???????? s8 -4.9880?????? FP1 A.K. ----- Original Message ----- From: simona mancini <mancinisimona at yahoo.it> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Friday, August 10, 2012 5:07 AM Subject: [R] subsetting levels of a vector Hi, I need to subset different levels of vector in a dataset to create a new dataframe that contains only these. These observations are not numerical, so I can't use the subset() function (at least this is the response I get from R). Suppose the dataframe looks like this: ? ParticipID??? ERP?? Electrode 1???????? s1? 0.0370?????? FP1 2???????? s2 35.0654?????? FP2 3???????? s3 -3.3852??????? F4 4???????? s4? 2.6119??????? P3 5???????? s5? 0.1224??????? P4 6???????? s6 -5.3153??????? O1 I want to create a subset of the dataframe with only the FP1, FP2, F4 levels of Electrode, how do I do? (actually the levels I have to filter are much more than these three, but this is just to give an example). I know this is a trivial question, but I am still getting familiar with R... Thanks a lot, s. ??? [[alternative HTML version deleted]] ______________________________________________ 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.