[R] Question about extracting certain rows from one column in a data.frame

R. Michael Weylandt michael.weylandt at gmail.com
Mon May 28 21:32:54 CEST 2012


On Mon, May 28, 2012 at 3:16 PM, Kelly Cool <kellycool79 at yahoo.com> wrote:
> I was wondering if there was a quick way to extract out certain rows from a data set in R?
> I have a data.frame, LOG,  where in one column, sample_data_tx, there is a list of 62 different types of treatment. I've sub-selected the rows that contain the names, "PLO" and "NOY" to make a new vector which I call, Test.
>
>  Here's my code so far,
>
> ##In LOG data set, Test set is every treatment, PLO and NOY##
>
> ##Select rows in the LOG data set that contain Noy##
>
> Noy <- which(LOG$sample_data_tx == "Noy")
>
> ##Select rows in the LOG data set that contain PLO##
>
> PLO <- which(LOG$sample_data_tx == "PLO")
>
> ##Make Test Set##
>  Test <- c(Noy, PLO)
>
>> Test
> [1]  8 24 50 23 29 46 55
>
>
> Within the data.frame, LOG, I would like to now make another vector, "Training", that contains every row in the column, "sample_data_tx", except rows 8, 24, 50, 23, 29, 46, 55.

I think you're looking for negative indexing (which is, in my opinion,
pretty much the best thing ever)

E.g.,

x <- letters[1:10]

x[1:3] # First three letters

x[-(1:3)] # Without the first three letters

x[-4] # Leave out "d"

etc.

Of course, for this case, you might also want the subset function:

subset(LOG, sample_data_tx %in% c("Noy","PLO"))

> "Test" is also an integer and I am hoping to make a hierarchical plot with both the "Test" and "Training" vectors so I am not sure if I first need to convert the data from integer to numeric form?

No, almost always these sorts of conversions will be taken care of you
automatically

Best,
Michael

>  I am new to R so all help is appreciated. Thanks in advance.
>        [[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.
>



More information about the R-help mailing list