Unlist a Data Frame by Rows, Not Columns

Unlist a data frame by rows, not columns

We can take the transpose (t) of the dataset and then use c to get a vector output

 c(t(df1))
#[1] 0 0 0 1 1 0 1 0 1 1 2 1 1 0 3

By doing transpose, we convert the 'data.frame' to 'matrix'. In both data.frame or matrix, unlist/c operations happen columnwise. So, transposing swaps the columns for rows and viceversa and we get the expected result.

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"

Unlist a data.frame column

For unlisting list-columns, you need to call unnest from the tidyr package.

unnest(dataframe, nameofcolumns)

Best,

Colin

Unlist mixed Dataframe into Rows. R

The tidyr function unnest can help you here.
The only problem you have is that your DATA frame contains a vector-of-lists in each column. If we convert the Names column to a character vector first you can unnest the Letters lists:

Names <- c("Andy", "Bill", "Carl", "Dave")
Letters <- c("A", list(c("A", "B", "C")), list(c("B", "C")), "B")
DATA <- as.data.frame(cbind(Names, Letters))

DATA
Names Letters
1 Andy A
2 Bill A, B, C
3 Carl B, C
4 Dave B

str(DATA)
'data.frame': 4 obs. of 2 variables:
$ Names :List of 4
..$ : chr "Andy"
..$ : chr "Bill"
..$ : chr "Carl"
..$ : chr "Dave"
$ Letters:List of 4
..$ : chr "A"
..$ : chr "A" "B" "C"
..$ : chr "B" "C"
..$ : chr "B"

So we convert the Names

DATA %>%
dplyr::mutate(Names = unlist(Names)) %>%
unnest()

Names Letters
1 Andy A
2 Bill A
3 Bill B
4 Bill C
5 Carl B
6 Carl C
7 Dave B

unlist my words into a df but keep all other columns?

We can just use separate_rows

library(tidyr)
separate_rows(df, V1)

-output

# A tibble: 3 x 3
# V1 V2 V3
# <chr> <chr> <int>
#1 I 24/12 3
#2 am 24/12 3
#3 happy 24/12 3

data

df <- structure(list(V1 = "I am happy", V2 = "24/12", V3 = 3L), 
class = "data.frame", row.names = c(NA, -1L))

Unlist a column of a data frame

You can use rowwise and unlist tags and positions column.

library(dplyr)

data %>%
rowwise() %>%
mutate(across(c(tags, positions), ~toString(unlist(.))))

# eventId subEventName tags playerId positions matchId eventName teamId matchPeriod eventSec subEventId id
# <dbl> <chr> <chr> <dbl> <chr> <dbl> <chr> <dbl> <chr> <dbl> <dbl> <dbl>
#1 8 Simple pass 1801 122671 50, 53, 50, 35 2057954 Pass 16521 1H 1.66 85 258612104
#2 8 High pass 1801 139393 53, 19, 35, 75 2057954 Pass 16521 1H 4.49 83 258612106
#3 1 Air duel 703, 1801 103668 81, 83, 25, 37 2057954 Duel 14358 1H 5.94 10 258612077
#4 1 Air duel 701, 1802 122940 19, 17, 75, 63 2057954 Duel 16521 1H 6.41 10 258612112
#5 8 Simple pass 1801 122847 17, 15, 63, 71 2057954 Pass 16521 1H 8.56 85 258612110

unlist and split a column to add to rows without losing information of other column in R

We can use separate_rows

library(tidyr)
separate_rows(df, col1, sep = ";")

If we want to use strsplit, then have to replicate the rows based on the length of list elements (lengths)

lst1 <- strsplit(df$col1, ";")
df1 <- df[rep(seq_len(nrow(df)), lengths(lst1)),]
df1$col1 <- unlist(lst1)

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


Related Topics



Leave a reply



Submit