[R] Splitting vector elements

Luca Cerone luca.cerone at gmail.com
Wed May 21 11:38:06 CEST 2014


Hi Katherin,
the following code does the trick:

x <- strsplit(dat," ")
df <- data.frame(p1=sapply(x, function(x) x[1]), p2 = sapply(x,
function(x) x[2]))

Hope it helps,
Luca

2014-05-21 11:29 GMT+02:00 Katherine Gobin <katherine_gobin at yahoo.com>:
> Dear R forum
>
> I have a vector as
>
> dat = c("ABC 1", "ABC 2", "ABC 3", "DEF 10", "DEF 20")
>
>> dat
> [1] "ABC 1"  "ABC 2"  "ABC 3"  "DEF 10" "DEF 20"
>
> I need to split the names into two parts say
>
>    p1      p2
>  ABC    1
>  ABC    2
>  ABC    3
>  DEF     10
>  DEF     20
>
> Kindly guide
>
> Katherine
>
>         [[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.
>



-- 
Luca Cerone

Tel: +34 692 06 71 28
Skype: luca.cerone



More information about the R-help mailing list