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

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Feb 7 09:20:14 CET 2008


hits=-2.5 tests=BAYES_00,FORGED_RCVD_HELO
X-USF-Spam-Flag: NO

try the following:

dat <- data.frame(x = c("sample1_condition1_place1",
    "sample2_condition1_place1", "sample3_condition1_place1",
    "sample1_condition2_place1", "sample1_condition2_place1"))

vals <- strsplit(as.character(dat$x), "_")
as.data.frame(do.call("rbind", vals))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Tribo Laboy" <tribolaboy at gmail.com>
To: <r-help at r-project.org>
Sent: Thursday, February 07, 2008 7:44 AM
Subject: [R] How to split a factor (unique identifier) into several 
others?


> Hello,
>
> I have a data frame with a factor column, which uniquely identifies
> the observations in the data frame and it looks like this:
>
> sample1_condition1_place1
> sample2_condition1_place1
> sample3_condition1_place1
> .
> .
> .
> sample3_condition3_place3
>
> 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]]
> [1] "sample1"    "condition1" "place1"
>
> [[2]]
> [1] "sample2"    "condition1" "place1"
>
> [[3]]
> [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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list