Skip to content
Back to formatted view

Raw Message

Message-ID: <6E8E2B87-32C6-4177-93D3-0652A4F64DA9@comcast.net>
Date: 2012-08-28T03:01:37Z
From: David Winsemius
Subject: ?nchar ?strsplit
In-Reply-To: <1346092830.97450.YahooMailNeo@web125205.mail.ne1.yahoo.com>

On Aug 27, 2012, at 1:40 PM, Sapana Lohani wrote:

> Hi, my data frame is
>
> x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))
>
> I want to split my column ID using "/" as the place to split. How  
> can I do that without telling the code how many sub-columns. I could  
> use nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row  
> of the column, but could not use it to split ID in.

 > read.table(text=as.character(x$ID), sep="/", fill=TRUE, as.is=TRUE)
    V1  V2  V3 V4
1 abc def
2 abc def ghi
3 abc
4 mno pqr  st ab

-- 

David Winsemius, MD
Alameda, CA, USA