简体   繁体   中英

Iterating through values in R

I'm new-ish to R and am having some trouble iterating through values.

For context: I have data on 60 people over time, and each person has his/her own dataset in a folder (I received the data with id #s 00:59). For each person, there are 2 values I need - time of response and picture response given (a number 1 - 16). I need to convert this data from wide to long format for each person, and then eventually append all of the datasets together.

My problem is that I'm having trouble writing a loop that will do this for each person (ie each dataset). Here's the code I have so far:

pam[x] <- fromJSON(file = "PAM_u[x].json")
pam[x]df <- as.data.frame(pam[x])

#Creating long dataframe for times
pam[x]_long_times <- gather(
select(pam[x]df, starts_with("resp")),
key = "time",
value = "resp_times"
)

#Creating long dataframe for pic_nums (affect response)
pam[x]_long_pics <- gather(
select(pam[x]df, starts_with("pic")),
key = "picture",
value = "pic_num"
)

#Combining the two long dataframes so that I have one df per person
pam[x]_long_fin <- bind_cols(pam[x]_long_times, pam[x]_long_pics) %>%
select(resp_times, pic_num) %>%
add_column(id = [x], .before = 1)

If you replace [x] in the above code with a person's id# (eg 00), the code will run and will give me the dataframe I want for that person. Any advice on how to do this so I can get all 60 people done?

Thanks!

EDIT So, using library(jsonlite) rather than library(rjson) set up the files in the format I needed without having to do all of the manipulation. Thanks all for the responses, but the solution was apparently much easier than I'd thought.

I don't know the structure of your json files. If you are not in the same folder, like the json files, try that:

library(jsonlite)

# setup - read files
json_folder <- "U:/test/" #adjust you folder here
files <- list.files(path = paste0(json_folder), pattern = "\\.json$")

# import data
pam <- NULL
pam_df <- NULL
for (i in seq_along(files)) {
    pam[[i]] <- fromJSON(file = files[i])
    pam_df[[i]] <- as.data.frame(pam[[i]])
}

Here you generally read all json files in the folder and build a vector of a length of 60. Than you sequence along that vector and read all files. I assume at the end you can do bind_rows or add you code in the for loop. But remember to set the data frames to NULL before the loop starts, eg pam_long_pics <- NULL

Hope that helped? Let me know.

Something along these lines could work:

#library("tidyverse")
#library("jsonlite")
file_list <- list.files(pattern = "*.json", full.names = TRUE)

Data_raw <- tibble(File_name = file_list) %>%
  mutate(File_contents = map(File_name, fromJSON)) %>% # This should result in a nested tibble
  mutate(File_contents = map(File_contents, as_tibble))    

Data_raw %>%
  mutate(Long_times = map(File_contents, ~ gather(key = "time", value = "resp_times", starts_with("resp"))), 
         Long_pics = map(File_contents, ~ gather(key = "picture", value = "pic_num", starts_with("pic")))) %>%
  unnest(Long_times, Long_pics) %>%
  select(File_name, resp_times, pic_num)

EDIT : you may or may not need not to include as_tibble() after reading in the JSON files, depending on how your data looks like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM