[R] How to split a factor (unique identifier) into several others?

From: Tribo Laboy <tribolaboy_at_gmail.com>
Date: Thu, 7 Feb 2008 15:44:10 +0900


I have a data frame with a factor column, which uniquely identifies the observations in the data frame and it looks like this:



I want to turn it into three separate factor columns "sample", "condition" and "place".

This is what I did so far:

# generate a factor column for the example fctr<- factor(c("sample1_condition1_place1", "sample2_condition1_place1", "sample3_condition1_place1")) splitfctr <- strsplit(as.character(fctr),"_")

> splitfctr

[1] "sample1" "condition1" "place1"

[1] "sample2" "condition1" "place1"

[1] "sample3" "condition1" "place1"

Now this is all fine, but how do I make three separate factors of this? The object "splitfctr" is a list of character vectors, each character vector being composed of the words after spitting the long original world.
Now I want to form new character vectors, which contain the first component of each list entry, then another vector for the second component, etc.
I don't want to use loops, unless that's the only way to do it.I guess I have some difficulty with understanding how R indexing works...

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. Received on Thu 07 Feb 2008 - 06:49:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Feb 2008 - 09:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive