logo
Tags down

shadow

Flatten JSON list into data frame


By : Mister Dahood
Date : October 17 2020, 08:10 PM
Hope this helps I have a directory of 143 JSON files (~30 MB), which I would like to combine and convert to a flat data frame. (This might be related to this question, but I couldn't get that solution to work. See bottom for results.) , Do you mean this?
code :
my_list %>%
    map(~ fromJSON(.x)) %>%
    bind_rows()
#  sometext_lets_pretend_its_JSON
#1                            999
#2                            999
#3                            999
#4                            999
#5                            999
#6                            999


Share : facebook icon twitter icon

R flatten out list hierarchy to matrix or data.frame


By : Nousheen Ahmad
Date : March 29 2020, 07:55 AM
will help you Here is a solution. So it reads easily here, I have broken the code into two parts. Later, you can easily merge the two parts into a single function.
First, a function that gets a matrix of all the names, using recursion:
code :
anames <- function(x) {

   require(plyr)
   if (is.data.frame(x)) return(NA)

   y <- do.call(rbind.fill.matrix,
                mapply(cbind, names(x), lapply(x, anames),
                       SIMPLIFY = FALSE))
   colnames(y) <- NULL

   return(y)
}

anames(a)
#      [,1] [,2] [,3] [,4]
# [1,] "b1" "c1" "d1" NA  
# [2,] "b1" "c2" NA   NA  
# [3,] "b2" NA   NA   NA
listToMatrix <- function(myList, steps = Inf) {

   a <- anames(myList)
   steps <- min(steps, ncol(a) - 1)
   cols.idx <- seq_len(steps)
   a <- a[, cols.idx]
   for (j in tail(cols.idx, -1))
      a[, j] <- ifelse(is.na(a[, j]), a[, j - 1], a[, j])

   return(a)
}

listToMatrix(a, 2)
#      [,1] [,2]
# [1,] "b1" "c1"
# [2,] "b1" "c2"
# [3,] "b2" "b2"

R - Flatten Variably Sized List of Lists to Data Frame?


By : Muhammet Akgün
Date : March 29 2020, 07:55 AM
around this issue I would like to flatten a list of variably-sized lists into a single data frame. This would be easy except for the fact that each list can have a different number of elements.
code :
library(plyr)
DF <- as.data.frame(
  do.call(rbind.fill.matrix,
          lapply(concertsList, function(l) {
            res <- unlist(l)
            names(res)[names(res)=="bands.band"] <- "bands.band1"
            t(res)
          })
  )
)

DF$concert <- names(concertsList)
names(DF) <- gsub("bands.","",names(DF))

#           band1       band2          venue  concert
#1   Foo Fighters Ace of Base concert hall 1 concert1
#2 The Black Keys        <NA> concert hall 2 concert2

Flatten nested list of lists with variable numbers of elements to a data frame


By : Alexandre Violeau
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , First the structure of the list needs to be reworked so it becomes a regular list of lists, then we apply map_dfr two times, using the .id parameter.
code :
library(purrr)
gc_all_df  <- map(map_if(gc_all,~class(.x)=="matrix",list),~map(.x,as.data.frame))
map_dfr(gc_all_df,~map_dfr(.x,identity,.id="id2"),identity,.id="id1")

How can I best flatten a nested list to a data.frame in R?


By : user3042922
Date : March 29 2020, 07:55 AM
around this issue I would like to convert information from a rest api, which comes in form of json, to a data.frame. The list is nested and theoretically I could repeatedly call purrr::flatten() to get to the bottom of the list and then extract the information using for example purrr:::map_dfr and magrittr:::extract. However, this is very domain specific and doesn't work nicely when extracting information from multiple "hierarchies". I have the following set-up in R: , You can unlist the result and extract x and y like this:
code :
res <- unlist(result)
res['results.attrs.x']
# results.attrs.x 
#  "151398.09375"

res['results.attrs.y']
# results.attrs.y 
#  "540429.3125"
names(res)
#[1] "results.id"  "results.weight"  "results.attrs.origin"         
#    "results.attrs.geom_quadindex" "results.attrs.zoomlevel"     
#[6] "results.attrs.featureId" "results.attrs.lon" "results.attrs.detail"   
#    "results.attrs.rank" "results.attrs.geom_st_box2d" "results.attrs.lat"
#    "results.attrs.num" "results.attrs.y" "results.attrs.x"  "results.attrs.label" 
res_df <- data.frame(
  X = res['results.attrs.x'],
  Y = res['results.attrs.y']
)

Flatten pandas data frame with a JSON column


By : Anurag Ekka
Date : March 29 2020, 07:55 AM
I hope this helps you . you can parse JSON in Pandas column using json.loads() and convert it to Pandas columns using pd.Series():
code :
In [85]: df.join(df.pop('col3').apply(lambda x: pd.Series(json.loads(x))))
Out[85]:
   col1         col2  col4 col3_1  col3_2
0     1  Programming    11   None    Java
1     2        Sport    22   None  Soccer
2     3         Food    33   None   Pizza
Related Posts Related Posts :
  • Finding the first non-zero year in data frame for multiple variables using tidyverse
  • ggplot2 - how to assign geom_text with arrow icon to second yaxis scale
  • regex fails with dollar sign
  • Drop first element of list of lists, condense list of lists? Too many elements?
  • R - how to apply output of ifelse(str_detect ...) to whole group
  • caret package confusion matrix define positive case with multiple classes
  • Generating a pairwise 'distance' matrix
  • Change all R columns names using a reference file
  • In R & dabestr, how do I get grouped differences correctly?
  • Exclude or set a unique color to the bottom triangle of a correlation matrix heatmap
  • r shiny observe function clears text input
  • Split column by multiple delimiters, keeping delimiters
  • How to random search in a specified grid in caret package?
  • merge 2 data frames in a loop for each column in one of them
  • how to edit the codes for the summary of R S4 Object?
  • Remove specific rows in R
  • Filtering a dataset and making a ggplot
  • Align cells vertically to be at the bottom flextable
  • R speed up sapply
  • invalid subscript type 'list' Azure Machine Learning
  • Use rollapply with xts object and an anonymous defined function
  • Isolate data frames from a spreadsheet to create a list
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • number similar/duplicated rows in R
  • Count the number of times each value appears in a row dataframe r
  • how to vectorise my code in r using for loop?
  • A function to fill in a column with NA of the same type
  • Network flow balancing constraint in R
  • Adding main titles from list to graphs in for loop
  • create a matrix in Perl or R if data is provided in CSV file
  • Passing column names as string to with
  • R - filtering rows and summing
  • How to change the order of fill aesthetic in faceted ggplot?
  • Function to remove outliers by group from dataframe
  • How to find the difference of max & min values in one group in a variable in a dataframe
  • Convert unicode to a readable string
  • Wrong scale/difficult to interpret times on time series object using 'ts'
  • Joining three numeric columns without adding them in r
  • Is there any way to extract the names of columns from an excel sheet without actually loading the sheet into the RAM?
  • case_when() not working: Error in mutate_impl(.data, dots)
  • Hide boxes if input not suitable in Shiny
  • Make nodes as images in R with visNetwork lib
  • Re-shape status columns based on value in another column
  • Why does the plot size differ between docx and html in rmarkdown::render?
  • Reverse x-axis that contains categorical data and a lot of annotations
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org