Martin Batholdy
2010-Feb-09 23:46 UTC
[R] split strings in a vector and convert it to a data.frame
hi, I have a vector full of strings like; xy_100_ab xy_101_ab xy_102_ab xy_103_ab I want to seperate each string in three pieces and the separator should be the "_" at the end I want a data.frame like: column1 column2 column3 xy 100 ab xy 101 ab xy 102 ab xy 103 ab I tried strsplit but I couldn't figure out how to convert the list I get into a data.frame. I just need a function like read.table. But it seems that read.table only can handle files, not vectors...
Jorge Ivan Velez
2010-Feb-09 23:51 UTC
[R] split strings in a vector and convert it to a data.frame
Hi Martin, Here is a sugestion: string <- c("xy_100_ab", "xy_101_ab","xy_102_ab","xy_103_ab") out <- data.frame( do.call( rbind, strsplit( string, '_' ) ) ) names(out) <- paste('column',1:3,sep="") out HTH, Jorge On Tue, Feb 9, 2010 at 6:46 PM, Martin Batholdy <> wrote:> hi, > > I have a vector full of strings like; > > > xy_100_ab xy_101_ab xy_102_ab xy_103_ab > > > I want to seperate each string in three pieces and the separator should be > the "_" > > at the end I want a data.frame like: > > column1 column2 column3 > xy 100 ab > xy 101 ab > xy 102 ab > xy 103 ab > > > I tried strsplit but I couldn't figure out how to convert the list I get > into a data.frame. > > I just need a function like read.table. But it seems that read.table only > can handle files, not vectors... > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
David Winsemius
2010-Feb-09 23:54 UTC
[R] split strings in a vector and convert it to a data.frame
On Feb 9, 2010, at 6:46 PM, Martin Batholdy wrote:> hi, > > I have a vector full of strings like; > > > xy_100_ab xy_101_ab xy_102_ab xy_103_ab > > > I want to seperate each string in three pieces and the separator > should be the "_" > > at the end I want a data.frame like: > > column1 column2 column3 > xy 100 ab > xy 101 ab > xy 102 ab > xy 103 ab >There are probably easier ways but this works: > as.data.frame( t(sapply(1:4, function(x) strsplit(vec, "_")[[x]])) ) V1 V2 V3 1 xy 100 ab 2 xy 101 ab 3 xy 102 ab 4 xy 103 ab I needed to transpose the matrix that resulted from the "sideways presentation" of the data.> > I tried strsplit but I couldn't figure out how to convert the list I > get into a data.frame. > > I just need a function like read.table. But it seems that read.table > only can handle files, not vectors... > ______________________________________________ > 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
Gabor Grothendieck
2010-Feb-10 00:52 UTC
[R] split strings in a vector and convert it to a data.frame
On Tue, Feb 9, 2010 at 6:46 PM, Martin Batholdy <batholdy at googlemail.com> wrote:> hi, > > I have a vector full of strings like; > > > xy_100_ab ? ? ? xy_101_ab ? ? ? xy_102_ab ? ? ? xy_103_ab > > > I want to seperate each string in three pieces and the separator should be the "_" > > at the end I want a data.frame like: > > column1 column2 column3 > xy ? ? ? ? ? ? ?100 ? ? ? ? ? ? ? ? ? ? ab > xy ? ? ? ? ? ? ?101 ? ? ? ? ? ? ? ? ? ? ab > xy ? ? ? ? ? ? ?102 ? ? ? ? ? ? ? ? ? ? ab > xy ? ? ? ? ? ? ?103 ? ? ? ? ? ? ? ? ? ? ab > > > I tried strsplit but I couldn't figure out how to convert the list I get into a data.frame. > > I just need a function like read.table. But it seems that read.table only can handle files, not vectors...Using textConnection() it can handle character vectors:> DF <- read.table(textConnection(x), sep = "_"); DFV1 V2 V3 1 xy 100 ab 2 xy 101 ab 3 xy 102 ab 4 xy 103 ab
Reasonably Related Threads
- Split data.frames depeding values of a column
- Function to read a string as the variables as opposed to taking the string name as the variable
- Displaying median value over the horizontal(median)line in the boxplot
- Reading sections of data files based on pattern matching
- New Variable from Several Existing Variables