1

I have a list and a list of lists and would like to create a data.frame or data.table.

Here is the list:

 head(stadte_namen)
[1] "Berlin"            "Hamburg"           "München"  

and a list of lists

> head(result)
[[1]]
       min      max
x 13.22886 13.54886
y 52.35704 52.67704

[[2]]
        min      max
x  9.840654 10.16065
y 53.390341 53.71034

[[3]]
       min      max
x 11.36078 11.72291
y 48.06162 48.24812

How could I create a data.frame or a data.table with the following structure?

name                xmin        ymin        xmax       ymax

Berlin              13.22886    52.35704    13.54886   52.67704
Hamburg             9.840654    53.390341   10.16065   53.71034
München             11.36078    48.06162    11.72291   48.24812
...

Here is the data:

stadte_namen<-c("Berlin", "Hamburg", "München", "Köln", "Frankfurt am Main", 
"Stuttgart")

result<-list(structure(c(13.2288599, 52.3570365, 13.5488599, 52.6770365
), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"), c("min", "max"
))), structure(c(9.840654, 53.390341, 10.160654, 53.710341), .Dim = c(2L, 
2L), .Dimnames = list(c("x", "y"), c("min", "max"))), structure(c(11.360777, 
48.0616244, 11.7229083, 48.2481162), .Dim = c(2L, 2L), .Dimnames = list(
    c("x", "y"), c("min", "max"))), structure(c(6.7725303, 50.8304399, 
7.162028, 51.0849743), .Dim = c(2L, 2L), .Dimnames = list(c("x", 
"y"), c("min", "max"))), structure(c(8.4727933, 50.0155435, 8.8004716, 
50.2271408), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"), 
    c("min", "max"))), structure(c(9.0386007, 48.6920188, 9.3160228, 
48.8663994), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"), 
    c("min", "max"))))

3 Answers 3

3

You can also try:

l <- result
df <- data.frame(t(sapply(l,c)))
colnames(df) <- c("minX", "minY", "maxX", "maxY"); df
df$stadte_namen <- c("Berlin", "Hamburg", "München", "Köln", "Frankfurt am Main", 
                     "Stuttgart");df

Answer:

       minX     minY      maxX     maxY      stadte_namen
1 13.228860 52.35704 13.548860 52.67704            Berlin
2  9.840654 53.39034 10.160654 53.71034           Hamburg
3 11.360777 48.06162 11.722908 48.24812           München
4  6.772530 50.83044  7.162028 51.08497              Köln
5  8.472793 50.01554  8.800472 50.22714 Frankfurt am Main
6  9.038601 48.69202  9.316023 48.86640         Stuttgart
Sign up to request clarification or add additional context in comments.

Comments

2

With lapply and purrr:

library(dplyr)
library(purrr)

data <- lapply(result, function(x) c(xmin = x[1,1],
                                     xmax = x[1,2],
                                     ymin = x[2,1],
                                     ymax = x[2,2])) %>%
        purrr::map_dfr(~.x)

data$stadte_namen <- stadte_namen

# A tibble: 6 x 5
   xmin  xmax  ymin  ymax stadte_namen     
  <dbl> <dbl> <dbl> <dbl> <chr>            
1 13.2  13.5   52.4  52.7 Berlin           
2  9.84 10.2   53.4  53.7 Hamburg          
3 11.4  11.7   48.1  48.2 München          
4  6.77  7.16  50.8  51.1 Köln             
5  8.47  8.80  50.0  50.2 Frankfurt am Main
6  9.04  9.32  48.7  48.9 Stuttgart

Comments

1

Assign stadte_namen as names to result and bind the dataframe together in one dataframe. You can get the data in wide format using pivot_wider.

library(tidyverse)

map_df(setNames(result, stadte_namen), ~.x %>% 
         as.data.frame %>%
         rownames_to_column('row'), .id = 'name') %>%
  pivot_wider(names_from = row, values_from = c(min, max))

#  name              min_x min_y max_x max_y
#  <chr>             <dbl> <dbl> <dbl> <dbl>
#1 Berlin            13.2   52.4 13.5   52.7
#2 Hamburg            9.84  53.4 10.2   53.7
#3 München           11.4   48.1 11.7   48.2
#4 Köln               6.77  50.8  7.16  51.1
#5 Frankfurt am Main  8.47  50.0  8.80  50.2
#6 Stuttgart          9.04  48.7  9.32  48.9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.