how to change value on nested list use map function in R

41 views Asked by At

Need help I have nested list, I would like to change all the value of the photo become empty list, the list are below :

df <- list(messaging = list (telegram = list (registered = 'true', photo = c(list()), LastSeen = '2023-03-03T01:54:31Z' ),
                             whatsapp = list (registered = 'true', photo = "dg872tg82f8gf782gf82", LastSeen = '2023-03-03T01:54:31Z', about = "John" ),
                             viber = list (registered = 'true', photo = "dg872tdewfrgggf782gf82", LastSeen = '2023-03-03T01:54:31Z', name = "John" )))

Right now i do it manual like this :

df$messaging$whatsapp$photo <- c(list())
df$messaging$viber$photo <- c(list())

My output expectation is like below picture but using map function because in the actual the list will have lot of nested list

enter image description here

Thanks for any help

1

There are 1 answers

0
Mark On BEST ANSWER
map(df, \(c1) map(c1, \(c2) map2(c2, names(c2), \(c3, n) if (n == "photo") c() else c3)))
  • for each child of the dataframe (c1) (messaging)
    • for each child of a c1 (c2) (telegram, whatsapp, viber)

      • for each child and name of a c2 (c3) (registered, photo, lastseen etc.)

        • if the name == "photo", return c(). Otherwise, return c3