Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flatten a data frame

I have this nested data frame

test <- structure(list(id = c(13, 27), seq = structure(list(
`1` = c("1997", "1997", "1997", "2007"),
`2` = c("2007", "2007", "2007", "2007", "2007", "2007", "2007")), 
.Names = c("1", "2"))), .Names = c("penr", 
"seq"), row.names = c("1", "2"), class = "data.frame")

I want a list of all values in the second column, namely

result <- c("1997", "1997", "1997", "2007", "2007", "2007", "2007", "2007", "2007", "2007", "2007")

Is there an easy way to achieve this?

like image 440
speendo Avatar asked Feb 27 '12 15:02

speendo


People also ask

How do you flatten a data frame?

The first method to flatten the pandas dataframe is through NumPy python package. There is a function in NumPy that is numpy. flatten() that perform this task. First, you have to convert the dataframe to numpy using the to_numpy() method and then apply the flatten() method.

What does flattening a DataFrame mean?

In this article, we are going to see how to flatten a list of DataFrames. Flattening is defined as Converting or Changing data format to a narrow format. The advantage of the flattened list is Increases the computing speed and Good understanding of data.

What does flatten do in pandas?

Index.flatten(order='C') Return a copy of the array collapsed into one dimension.

How do I flatten a column in a DataFrame?

Flatten columns: use get_level_values() Flatten columns: use to_flat_index() Flatten columns: join column labels. Flatten rows: flatten all levels.


1 Answers

This line does the trick:

do.call("c", test[["seq"]])

or equivalent:

c(test[["seq"]], recursive = TRUE)

or even:

unlist(test[["seq"]])

The output of these functions is:

    11     12     13     14     21     22     23     24     25     26     27 
"1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007" "2007" 

To get rid of the names above the character vector, call as.character on the resulting object:

> as.character((unlist(test[["seq"]])))
 [1] "1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007"
[11] "2007"
like image 189
Paul Hiemstra Avatar answered Sep 28 '22 09:09

Paul Hiemstra