-1

I have an object of the following structure, which is not that common for me (with a dataframe within a dataframe with a list in it). I want to add new observations, which all have the same values for all variables (except 1); here the values come from a extra list via index). The nested structure of the object makes it difficult to do this with a loop. Therefore I copied the output of dput and entered values into it. I used mainly the function rep() and once unlist(). Executing it and printing the object I get a warning message about a corrupt a dataframe and the object hasn't changed.

Which is the common way to add rows to a given structure? I tried looping over it but hadn't had success yet.

deput(x)

structure(list(collapsed = c(FALSE, FALSE, FALSE), enabled = c(TRUE, 
TRUE, TRUE), id = c("Bilateral", "AlphaTree", "Merge"), parameters = structure(list(
    bands = list(c(1L, 2L, 3L, 5L), c(1L, 2L, 3L, 5L), NULL), 
    intensity = c(3L, NA, NA), saveToDisk = c(FALSE, NA, NA), 
    Strategy = c(NA, "maxDiff", NA), Attribute = c(NA, 
    "", NA), om = c(NA, 27L, NA), Condition = c(NA, NA, 
    "num < 10"), check = c(NA, NA, FALSE), ite = c(NA, 
    NA, 10L), Algo = c(NA, NA, "closest mean")), class = "data.frame", row.names = c(NA, 
3L)), type = c("PreProc", "Seg", "Op")), class = "data.frame", row.names = c(NA, 
3L))
TAKOPF
  • 1
  • 1
  • 1
    @akrun the dput was a very helpful already! Thanks – TAKOPF Dec 30 '21 at 20:34
  • Not clear about your expected output. if you want to add a row with only some column values, `library(tibble);library(dplyr); x %>% add_row(collapsed = FALSE, enabled = TRUE)` – akrun Dec 30 '21 at 20:36
  • Also, as there is a `data.frame` column, it can be `unpack`ed i.e. `library(tidyr);x %>% unpack(parameters)` before adding the row – akrun Dec 30 '21 at 20:37
  • Could you show how to use the unpack function here? How to combine unpack and add_row. When I use unpack for parameters I get this error: Error: Must subset columns with a valid subscript vector. x Subscript has the wrong type `list`. – TAKOPF Dec 30 '21 at 21:55
  • That error could be due to some package version issues as I didn't get an error with ` packageVersion('tidyr') [1] ‘1.1.3’` – akrun Dec 30 '21 at 21:56
  • And how is it possible to "pack" it again? x%>%unpack(parameters)%>%add_row(collapsed = FALSE)%>%pack(parameters) ??? – TAKOPF Dec 30 '21 at 22:34
  • Your expected output is not clear to me. If you had updated your post, it would have been helpful – akrun Dec 30 '21 at 22:34
  • Do you want `x%>%unpack(parameters)%>%add_row(collapsed = FALSE) %>% pack(parameters = names(x$parameters))` – akrun Dec 30 '21 at 22:37
  • I posted my comments as a solution. It works for me – akrun Dec 30 '21 at 22:44

1 Answers1

1

We may use add_row from tibble - the column values that are not passed in the add_row will be NA for that row or if it is a list will turn to NULL

library(dplyr)
library(tibble)
x1 <- x %>%
    add_row(collapsed = FALSE)

-output

x1
  collapsed enabled        id parameters.bands parameters.intensity parameters.saveToDisk parameters.Strategy parameters.Attribute parameters.om
1     FALSE    TRUE Bilateral       1, 2, 3, 5                    3                 FALSE                <NA>                 <NA>            NA
2     FALSE    TRUE AlphaTree       1, 2, 3, 5                   NA                    NA             maxDiff                                 27
3     FALSE    TRUE     Merge             NULL                   NA                    NA                <NA>                 <NA>            NA
4     FALSE      NA      <NA>             NULL                   NA                    NA                <NA>                 <NA>            NA
  parameters.Condition parameters.check parameters.ite parameters.Algo    type
1                 <NA>               NA             NA            <NA> PreProc
2                 <NA>               NA             NA            <NA>     Seg
3             num < 10            FALSE             10    closest mean      Op
4                 <NA>               NA             NA            <NA>    <NA>
> str(x1)
'data.frame':   4 obs. of  5 variables:
 $ collapsed : logi  FALSE FALSE FALSE FALSE
 $ enabled   : logi  TRUE TRUE TRUE NA
 $ id        : chr  "Bilateral" "AlphaTree" "Merge" NA
 $ parameters:'data.frame': 4 obs. of  10 variables:
  ..$ bands     :List of 4
  .. ..$ : int  1 2 3 5
  .. ..$ : int  1 2 3 5
  .. ..$ : NULL
  .. ..$ : NULL
  ..$ intensity : int  3 NA NA NA
  ..$ saveToDisk: logi  FALSE NA NA NA
  ..$ Strategy  : chr  NA "maxDiff" NA NA
  ..$ Attribute : chr  NA "" NA NA
  ..$ om        : int  NA 27 NA NA
  ..$ Condition : chr  NA NA "num < 10" NA
  ..$ check     : logi  NA NA FALSE NA
  ..$ ite       : int  NA NA 10 NA
  ..$ Algo      : chr  NA NA "closest mean" NA
 $ type      : chr  "PreProc" "Seg" "Op" NA

If we want to add values to some elements in the data.frame column, then unpack add values and then pack it again

library(tidyr)
x1 <- x%>%
     unpack(parameters)%>%
     add_row(collapsed = FALSE, Condition = "num > 5" ) %>%
     pack(parameters = names(x$parameters))

-output

> x1
# A tibble: 4 × 5
  collapsed enabled id        type    parameters$bands $intensity $saveToDisk $Strategy $Attribute   $om $Condition $check  $ite $Algo       
  <lgl>     <lgl>   <chr>     <chr>   <list>                <int> <lgl>       <chr>     <chr>      <int> <chr>      <lgl>  <int> <chr>       
1 FALSE     TRUE    Bilateral PreProc <int [4]>                 3 FALSE       <NA>       <NA>         NA <NA>       NA        NA <NA>        
2 FALSE     TRUE    AlphaTree Seg     <int [4]>                NA NA          maxDiff   ""            27 <NA>       NA        NA <NA>        
3 FALSE     TRUE    Merge     Op      <NULL>                   NA NA          <NA>       <NA>         NA num < 10   FALSE     10 closest mean
4 FALSE     NA      <NA>      <NA>    <NULL>                   NA NA          <NA>       <NA>         NA num > 5    NA        NA <NA>        
> str(x1)
tibble [4 × 5] (S3: tbl_df/tbl/data.frame)
 $ collapsed : logi [1:4] FALSE FALSE FALSE FALSE
 $ enabled   : logi [1:4] TRUE TRUE TRUE NA
 $ id        : chr [1:4] "Bilateral" "AlphaTree" "Merge" NA
 $ type      : chr [1:4] "PreProc" "Seg" "Op" NA
 $ parameters: tibble [4 × 10] (S3: tbl_df/tbl/data.frame)
  ..$ bands     :List of 4
  .. ..$ : int [1:4] 1 2 3 5
  .. ..$ : int [1:4] 1 2 3 5
  .. ..$ : NULL
  .. ..$ : NULL
  ..$ intensity : int [1:4] 3 NA NA NA
  ..$ saveToDisk: logi [1:4] FALSE NA NA NA
  ..$ Strategy  : chr [1:4] NA "maxDiff" NA NA
  ..$ Attribute : chr [1:4] NA "" NA NA
  ..$ om        : int [1:4] NA 27 NA NA
  ..$ Condition : chr [1:4] NA NA "num < 10" "num > 5"
  ..$ check     : logi [1:4] NA NA FALSE NA
  ..$ ite       : int [1:4] NA NA 10 NA
  ..$ Algo      : chr [1:4] NA NA "closest mean" NA
akrun
  • 874,273
  • 37
  • 540
  • 662