1

Given the following data (myinput.txt):

A  q,y,h
B  y,f,g
C  n,r,q
### more rows

How can I convert it into such data structure in R?

$A
 [1] "q" "y" "h" 
$B
 [1] "y" "f" "g"
$C
 [1] "n" "r" "q" 
0

2 Answers 2

4

I've assumed this as your data:

dat <- read.table(text="q,y,h
y,f,g
n,r,q", header=FALSE, sep=",", row.names=c("A", "B", "C"))

If you want an automatic method:

as.list(as.data.frame((t(dat)), stringsAsFactors=FALSE))

## $A
## [1] "q" "y" "h"
##
## $B
## [1] "y" "f" "g"
## 
## $C
## [1] "n" "r" "q"

Another couple of methods which work are:

lapply(apply(dat, 1, list), "[[", 1)

unlist(apply(dat, 1, list), recursive=FALSE)
Sign up to request clarification or add additional context in comments.

2 Comments

@sebastian-c: thanks a lot. Is there a way I can make 'dat' automatically recognize the row.names? i.e. not assigning it.
@neversaint I just did that to recreate your data. I should have used row.names=1, so an example would be: read.csv("dat.csv", row.names=1). You might also want to add either colClasses="character" or stringsAsFactors=FALSE into the read.table.
0

Using a bit of readLines strsplit and regex to account for breaking the names off the start:

dat <- readLines(textConnection("A  q,y,h
B  y,f,g
C  n,r,q"))

result <- lapply(strsplit(dat,"\\s{2}|,"),function(x) x[2:length(x)])
names(result) <- gsub("^(.+)\\s{2}.+$","\\1",dat)

> result
$A
[1] "q" "y" "h"

$B
[1] "y" "f" "g"

$C
[1] "n" "r" "q"

or with less regex and more steps:

result <- strsplit(dat,"\\s{2}|,")
names(result) <- lapply(result,"[",1)
result <- lapply(result,function(x) x[2:length(x)])

> result
$A
[1] "q" "y" "h"

$B
[1] "y" "f" "g"

$C
[1] "n" "r" "q"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.