简体   繁体   English

遍历R中的值

[英]Iterating through values in R

I'm new-ish to R and am having some trouble iterating through values. 我是R的新手,在遍历值时遇到一些麻烦。

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). 对于上下文:随着时间的推移,我有60个人的数据,每个人的文件夹中都有自己的数据集(我收到的ID为#s 00:59的数据)。 For each person, there are 2 values I need - time of response and picture response given (a number 1 - 16). 对于每个人,我需要2个值-给出的响应时间和图片响应(数字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. 如果将上述代码中的[x]替换为一个人的ID#(例如00),该代码将运行并为我提供该人想要的数据框。 Any advice on how to do this so I can get all 60 people done? 关于如何执行此操作的任何建议,以便我可以完成全部60个人的工作?

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. 编辑因此,使用library(jsonlite)而不是library(rjson)以我需要的格式设置文件,而无需执行所有操作。 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. 我不知道您的json文件的结构。 If you are not in the same folder, like the json files, try that: 如果您不在json文件之类的同一个文件夹中,请尝试以下操作:

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. 在这里,您通常会读取该文件夹中的所有json文件,并构建一个长度为60的向量。然后,沿着该向量排序并读取所有文件。 I assume at the end you can do bind_rows or add you code in the for loop. 我假设最后可以执行bind_rows或在for循环中添加代码。 But remember to set the data frames to NULL before the loop starts, eg pam_long_pics <- NULL 但是请记住在循环开始之前将数据帧设置为NULL ,例如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. 编辑 :在读取JSON文件后,您可能需要也可能不需要包含as_tibble() ,具体取决于您的数据外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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