[R] Split column

David Winsemius dwinsemius at comcast.net
Wed Nov 25 00:05:59 CET 2009


The solution I offered does exactly that. It also addresses your other  
supplemental request.

-- 
David (Dangerfield?)

On Nov 24, 2009, at 5:57 PM, Lisaj wrote:

>
> I have a further question. If there is NA (missing data) in the  
> original
> dataset, just like this:
>
> id   var1   var2
> 1      1      3
> 2      3      1
> 3      NA    1
> 4      1      2
> 5      2      3
>
> how to deal with it? The converted dataset should be this:
>
> id   var1.1  var1.2  var2.1  var2.2
> 1         1       1       2       2
> 2         2       2       1       1
> 3         NA     NA     1       1
> 4         1       1       1       2
> 5         1       2       2       2
>
> Thanks.
>
> Lisa
>
> The
>
> Henrique Dallazuanna wrote:
>>
>> Try this:
>>
>> cbind(x$id, t(do.call(rbind, lapply(x[-1], sapply, switch, '1' =
>> c(1,1), '2' = c(1, 2), '3' = c(2, 2)))))
>>
>> On Tue, Nov 24, 2009 at 1:51 PM, Lisaj <lisajca at gmail.com> wrote:
>>>
>>> Hello, R users,
>>>
>>> I have a dataset that looks like this:
>>>
>>> id   var1   var2
>>>  1      1      3
>>>  2      3      1
>>>  3      2      1
>>>  4      1      2
>>>  5      2      3
>>>
>>> I want to split one column to two columns with 1 = 1 and 1, 2 = 1  
>>> and 2,
>>> 3 =
>>> 2 and 2:
>>>
>>> id   var1.1  var1.2  var2.1  var2.2
>>> 1         1       1       2       2
>>> 2         2       2       1       1
>>> 3         1       2       1       1
>>> 4         1       1       1       2
>>> 5         1       2       2       2
>>>
>>> Can anyone please help how to get this done? Thanks a lot in advance
>>>
>>> Lisa
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Split-column-tp26498361p26498361.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> -- 
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>>
>> ______________________________________________
>> 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.
>>
>>
>
> -- 
> View this message in context: http://old.nabble.com/Split-column-tp26498361p26504785.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list