开发者

How can I get specific data out of a list (with lists) under a certain condition

开发者 https://www.devze.com 2022-12-07 17:15 出处:网络
I have lists in lists and would like to take the activities that occur just within the first 600 seconds (journey time < 600). The "journey time" starts with 0 and adds the time of corres

I have lists in lists and would like to take the activities that occur just within the first 600 seconds (journey time < 600). The "journey time" starts with 0 and adds the time of corresponding activity "code" on top.

homepage1[["customer_data"]][["activity_list"]][[i]][["journey_time"]]

homepage1[["customer_data&开发者_JS百科quot;]][["activity_list"]][[i]][["code"]]

So for example [["journey_time"]] could look like this 0, 46.7, 79.4, ...., 1800.

[["code"]] looks like StartPage, ClickItem1, ScrollItem1, ..., ClosePage.

"i" are the customers here.

I tried it for each customer alone, but I, of course, would prefer an iterative process with loops.

Thank you in advance! Appreciate it much!

Marius


This should work.

homepage1 <- list(
  customer_data = list(
    activity_list = list(
      list(journey_time = 0, code = "StartPage"),
      list(journey_time = 46.7, code = "ClickItem1"),
      list(journey_time = 79.4, code = "ScrollItem1"),
      list(journey_time = 1800, code = "ClosePage")
    )
  )
)

# create an empty list to store the activity codes
activity_codes <- list()

# iterate over each element in the list of lists
for (i in 1:length(homepage1[["customer_data"]][["activity_list"]])) {
  # check if the journey time is less than 600
  if (homepage1[["customer_data"]][["activity_list"]][[i]][["journey_time"]] < 600) {
    # if it is, add the activity code to the list
    activity_codes <- c(activity_codes, homepage1[["customer_data"]][["activity_list"]][[i]][["code"]])
  }
}

# print the list of activity codes
print(activity_codes)

Edit: Updated from python to r as mentioned by @Maël

0

精彩评论

暂无评论...
验证码 换一张
取 消