Create dataframe from list of lists in R [duplicate] - r

This question already has answers here:
R list of lists to data.frame
(6 answers)
Closed 6 years ago.
I have a variable out that is a list of lists and I want to format the first child list to a dataframe. Say my out looks like this:
[[1]]
[[1]]$id
[1] "1"
[[1]]$input
[1] "A" "B" "C"
[[2]]
[[2]]$id
[1] "2"
[[2]]$input
[1] "R" "S" "T"
class(out) and class(out[[1]]) confirms that this is a list of lists.
I want to create a "long" dataframe that should look like this:
id input
1 "A"
1 "B"
1 "C"
2 "R"
2 "S"
2 "T"
I tried:
lapply(out, function(x){
as.data.frame(x)
})
but this seems to do an cbind and creates new columns for each child list.
Any help is highly appreciated.

try
library(plyr)
ldply(out, as.data.frame)

Related

Two Column R Dataframe to Named LIst [duplicate]

This question already has answers here:
Named List To/From Data.Frame
(4 answers)
Closed 2 years ago.
I am trying to convert a two-column dataframe to a named list. There are several solutions on StackOverflow where every value in the first column becomes the 'name', but I am looking to collapse the values in column 2 into common values in column 1.
For example, the list should look like the following:
# Create a Named list of keywords associated with each file.
fileKeywords <- list(fooBar.R = c("A","B","C"),
driver.R = c("A","F","G"))
Where I can retrieve all keywords for "fooBar.R" using:
# Get the keywords for a named file
fileKeywords[["fooBar.R"]]
My data frame looks like:
df <- read.table(header = TRUE, text = "
file keyWord
'fooBar.R' 'A'
'fooBar.R' 'B'
'fooBar.R' 'C'
'driver.R' 'A'
'driver.R' 'F'
'driver.R' 'G'
")
I'm sure there is a simple solution that I am missing.
You could use unstack:
as.list(unstack(rev(df)))
$driver.R
[1] "A" "F" "G"
$fooBar.R
[1] "A" "B" "C"
This is equivalent to as.list(unstack(df, keyWord~file))
We can use stack in base R
stack(fileKeywords)[2:1]
if it is the opposite, then we can do
with(df, tapply(keyWord, file, FUN = I))
-output
#$driver.R
#[1] "A" "F" "G"
#$fooBar.R
#[1] "A" "B" "C"

Concatenate two vectors while preserving order in R [duplicate]

This question already has answers here:
Interlacing two vectors [duplicate]
(2 answers)
Closed 3 years ago.
This is hard to explain, so I'll try and then leave a simple example. When I concatenate the vectors, I would like the first element of each vector next to each other, then the second elements next to each other, etc. See example below.
x <- c("a","b","c")
y <- c(1,2,3)
c(x,y)
[1] "a" "b" "c" "1" "2" "3"
However, I would like the following:
[1] "a" "1" "b" "2" "c" "3"
I'm sure there is an answer on here already, but I'm having trouble putting in the right search. Any help appreciated!
An option would be to rbind and then concatenate
c(rbind(x, y))
#[1] "a" "1" "b" "2" "c" "3"
and for general case when the vectors are not of same length, order on the sequence of elements concatentated
c(x, y)[order(c(seq_along(x), seq_along(y)))]
#[1] "a" "1" "b" "2" "c" "3"

return number of specific element of vector based of its name [duplicate]

This question already has answers here:
Convert letters to numbers
(5 answers)
Closed 5 years ago.
I need to return number of element in vector based on vector element name. Lets say i have vector of letters:
myLetters=letters[1:26]
> myLetters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
and what I intent to do is to create/find function that returns me the number of element when called for example:
myFunction(myLetters["b"])
[1] 2
myFunction(myLetters["z"])
[1]26
In summary I need a way to refer to excel columns by writing letters of a column (A,B,C later maybe even AA or further) and to get the number.
If you want to refer to excel columnnames, you could create a reference vector with all possible excel column names:
eg1 <- expand.grid(LETTERS, LETTERS)
eg2 <- expand.grid(LETTERS, LETTERS, LETTERS)
excelcols <- c(LETTERS, paste0(eg1[[2]], eg1[[1]]), paste0(paste0(eg2[[3]], eg2[[2]], eg2[[1]])))
After which you can use which:
> which(excelcols == 'A')
[1] 1
> which(excelcols == 'AB')
[1] 28
> which(excelcols == 'ABC')
[1] 731
If you need to find the number of times specific letter occurs then the following should work:
myLetters = c("a","a", "b")
myFunction = function(myLetters, findLetter){
length(which(myLetters==findLetter))
}
Let find how many times "a" occurs in myLetters:
myFunction(myLetters, "a")
# [1] 2

Delete list conditional on the number of elements in R

I have a list L of unnamed comma separated character lists. Each list of characters is of unequal length. I need to drop the character lists that have less than 4 elements from L. How can this be done? Example L:
> L
[[1]]
[1] "A" "B" "C" "D"
[[2]]
[1] "E" "F" "G"
In the example above I would like to end up with:
> L
[[1]]
[1] "A" "B" "C" "D"
We can use lengths to get the length of the list elements as a vector, create a logical vector based on that and subset the list
L[lengths(L)>3]
#[[1]]
#[1] "A" "B" "C" "D"
A less optimized approach (used earlier) is to loop through the list elements with sapply, get the length and use that to subset
L[sapply(L, length)>3]
data
L <- list(LETTERS[1:4], LETTERS[5:7])

Looking for interactions of all elements of a list (in R) [duplicate]

This question already has answers here:
How to generate all possible combinations of vectors without caring for order?
(2 answers)
Closed 9 years ago.
I am looking for a function that takes one list (of numbers, characters or any kind of objects) and returns two vectors (or a list of vectors) which represents all possible interactions (we might apply the function table(..) on these outputs).
> my.fun(list(1,2)) # returns the following
> c(1)
> c(2)
> my.fun(list('a','b','c')) # returns the following
> c('a','a','b')
> c('b','c','c')
> my.fun(list('a','b','c','d')) # returns the following
> c('a','a','a','b','b','c')
> c('b','c','d','c','d','d')
Does it make sense?
Use combn:
f <- function(L) {
i <- combn(length(L), 2)
list(unlist(L[i[1, ]]), unlist(L[i[2, ]]))
}
then,
> f(list(1,2))
[[1]]
[1] 1
[[2]]
[1] 2
> f(list('a','b','c'))
[[1]]
[1] "a" "a" "b"
[[2]]
[1] "b" "c" "c"
> f(list('a','b','c','d'))
[[1]]
[1] "a" "a" "a" "b" "b" "c"
[[2]]
[1] "b" "c" "d" "c" "d" "d"

Resources