Reputation: 4229
Sample data:
ht <- list(c("word1","word2"),c("number1","number2"))
First, strsplit()
requires comma, but I'm failing to insert comma between the strings.
I have tried something like;
lapply(ht, function(x) paste("",",",x)
Anyway, the desired output:
Words Numbers
1 word1 number1
2 word2 numner2
where Words
and Numbers
are column names.
EDIT: This is what I came up with, but in one column.
seplist <- lapply(splitString, function(x) paste(shQuote(x, type="cmd"), collapse=","))
unls <- data.table(unlist(seplist))
head(unls)
How could I proceed from here to the desired outcome?
EDIT 2: The data I have look like this:
[[1]]
[1] "WIELANDER, s.r.o." "Milochov 223 01706 Považská Bystrica"
[[2]]
[1] "Vojenský technický ústav, a.s." "kpt. Nálepku 03101 Liptovský Mikuláš"
[[3]]
[1] "Property Service, s.r.o." "Dlhá 25A 90031 Stupava"
Upvotes: 0
Views: 109
Reputation: 3597
With lapply
,do.call
and unlist
df<-do.call(cbind,lapply(ht,function(x) data.frame(x=unlist(x),stringsAsFactors=FALSE)))
names(df)<-c("Words","Numbers")
> df
Words Numbers
1 word1 number1
2 word2 number2
Upvotes: 0
Reputation: 99391
> ht <- list(c("word1","word2"), c("number1","number2"))
> x <- data.frame(do.call(cbind, ht))
> names(x) <- c('Words', 'Numbers')
> x
Words Numbers
1 word1 number1
2 word2 number2
Of course, this would be even more simple if ht
was a named list.
> ht <- list(Words = c("word1","word2"), Numbers = c("number1","number2"))
> data.frame(do.call(cbind, ht))
Words Numbers
1 word1 number1
2 word2 number2
Upvotes: 1
Reputation: 13076
Make use of the fact that a data.frame in R is a list of atomic vectors (each column = separate list item). This means that you almost have what you want.
structure(as.data.frame(ht), names=c('Words', 'Numbers'))
## Words Numbers
## 1 word1 number1
## 2 word2 number2
Upvotes: 3