Splitting vector elements

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Splitting vector elements

Katherine Gobin
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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Splitting vector elements

Luca Cerone
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 <[hidden email]>:

> 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]]
>
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Splitting vector elements

arun kirshna


Hi Katherine,

#or you can use:
read.table(text=dat,sep="",header=FALSE,stringsAsFactors=FALSE)
#   V1 V2
#1 ABC  1
#2 ABC  2
#3 ABC  3
#4 DEF 10
#5 DEF 20
A.K.


On Wednesday, May 21, 2014 5:38 AM, Luca Cerone <[hidden email]> wrote:
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 <[hidden email]>:

> 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]]
>
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Splitting vector elements

Luca Cerone
Thanks Arun,
I agree using read.table is probably easier :)

2014-05-21 12:08 GMT+02:00 arun <[hidden email]>:

>
>
> Hi Katherine,
>
> #or you can use:
> read.table(text=dat,sep="",header=FALSE,stringsAsFactors=FALSE)
> #   V1 V2
> #1 ABC  1
> #2 ABC  2
> #3 ABC  3
> #4 DEF 10
> #5 DEF 20
> A.K.
>
>
> On Wednesday, May 21, 2014 5:38 AM, Luca Cerone <[hidden email]> wrote:
> 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 <[hidden email]>:
>> 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]]
>>
>>
>> ______________________________________________
>> [hidden email] 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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.