R Unlist Changes Names

R unlist changes names

We can try setting the names after unlisting. We also use use.names=FALSE to avoid creating and rewriting a vector of names (MartinMorgan):

setNames(unlist(l, use.names=F),rep(names(l), lengths(l)))
#foo123 foo123 foo123 foo456 foo789
# 1 2 3 5 8

However, note that in most cases, duplicate names will lead to ambiguity and possibly errors. Given your example, if we now attempt to subset using the name "foo123" R outputs only the first instance:

o <- setNames(unlist(l, use.names=F),rep(names(l), lengths(l)))
o["foo123"]
# foo123
# 1

Unlist and retain list names intact in R

library(reshape2)

melt(FrameData)
#Using as id variables
#Using as id variables
# variable value L1
#1 V1 1 5555
#2 V1 1 5555
#3 V2 1 5555
#4 V2 1 5555
#5 V3 3 5555
#6 V3 3 5555
#7 V4 3 5555
#8 V4 3 5555
#9 V1 1 5566
#10 V1 1 5566
#11 V2 1 5566
#12 V2 1 5566
#13 V3 2 5566
#14 V3 2 5566
#15 V4 2 5566
#16 V4 2 5566

How to unlist a list and keep the names of the top level as a new variable in R

dplyrs bind_rows should do the trick:

library(dplyr)

bind_rows(my_list, .id = "Ticker")

This returns

# A tibble: 30 x 3
Ticker date Value
<chr> <date> <int>
1 Ticker1 2021-01-01 1
2 Ticker1 2021-01-02 2
3 Ticker1 2021-01-03 3
4 Ticker1 2021-01-04 4
5 Ticker1 2021-01-05 5
6 Ticker1 2021-01-06 6
7 Ticker1 2021-01-07 7
8 Ticker1 2021-01-08 8
9 Ticker1 2021-01-09 9
10 Ticker1 2021-01-10 10
# ... with 20 more rows

Unlisting a data frame by changing column names of the data frames

Here's a mockup of what I was describing in the comments. I'm not showing list2env because that seems unnecessary to your end goal.

Sample data:

myList <- replicate(10, data.frame(v1 = 1:2, v2 = 3:4), FALSE)

Renaming columns and writing the data to csv files:

lapply(seq_along(myList), function(x) {
FileName <- sprintf("%s.csv", letters[x])
write.csv(setNames(myList[[x]], paste0(letters[x], 1:length(myList[[x]]))),
file = FileName, row.names = FALSE)
})

The result would be 10 csv files, named a.csv, b.csv, and so on, wherein a.csv will have columns named "a1" ... "an".

How to avoid unlist() modification of list naming

unlist(unname(list1))
# 1 2 2 3 4 4 5 6 6 6
# 1 2 3 4 5 6 7 8 9 10

Unlist nested list by name

Using Map, avoiding the names vector.

data.frame(Map(unlist, nested_list)[1])
# iter1
# 1 1
# 2 2
# 3 3
# 4 4

Or, in order to give column names with mapply:

data.frame(x=mapply(unlist, nested_list)[,1])
# x
# 1 1
# 2 2
# 3 3
# 4 4

The 1 in brackets indicates first list name, use 2 for the second name accordingly.


Data

nested_list <- list(iter1 = list(1, 2, 3, 4), iter2 = list(1, 2, 3, 4))

Unlist lists of dataframes and assign names on a loop - R

We can use get to get the value of the data

for (i in May){

Date <- paste("2021-05", i, sep = "-")
List <- paste0("Data_", Date)
File <- get(List)[["df1"]]
DfName <- paste("df1", Date, sep = "_")

assign(DfName, File)
}

Another option is to use mget to return all objects in a list, loop over the list with lapply, extract the 'df1' (it is recommended to keep it in a list instead of creating multiple objects), but if we need objects, use list2env

lst1 <- lapply(mget(paste0("Data_2021-05-", May)), `[[`, "df1")
names(lst1) <- paste0("df1", names(lst1))
list2env(lst1, .GlobalEnv)

Unlist data frame column preserving information from other column

Here, the idea is to first get the length of each list element using sapply and then use rep to replicate the col1 with that length

 l1 <- sapply(myDataFrame$col2, length)
unlist.col1 <- rep(myDataFrame$col1, l1)
unlist.col1
#[1] "A" "A" "A" "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D"

Or as suggested by @Ananda Mahto, the above could be also done with vapply

   with(myDataFrame, rep(col1, vapply(col2, length, 1L)))
#[1] "A" "A" "A" "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D"

R Get list name when unlisting

We can use rep along with lengths where we repeat the name of the list according to number of elements in it.

rep(names(test), lengths(test))
#[1] "30875" "30875" "30875" "30876" "30876" "30876" "30876"

To put it in data.table

library(data.table)
data.table(A=unlist(test), B=rep(names(test), lengths(test)))

# A B
#1: hello 30875
#2: world 30875
#3: ! 30875
#4: Nice 30876
#5: to 30876
#6: meet 30876
#7: you 30876

How to unlist a list of mixed types without losing those types?

If you just want to print the elements, you could use lapply and dput:

invisible(lapply(list("Alice",c(11L,12L),64,c(11,13,12),list(A = 2,B = 3)), dput))
#"Alice"
#11:12
#64
#c(11, 13, 12)
#list(A = 2, B = 3)


Related Topics



Leave a reply



Submit