繁体   English   中英

遍历R中的值

[英]Iterating through values in R

我是R的新手,在遍历值时遇到一些麻烦。

对于上下文:随着时间的推移,我有60个人的数据,每个人的文件夹中都有自己的数据集(我收到的ID为#s 00:59的数据)。 对于每个人,我需要2个值-给出的响应时间和图片响应(数字1-16)。 我需要将每个人的数据从宽格式转换为长格式,然后最终将所有数据集附加在一起。

我的问题是我在编写一个将为每个人(即每个数据集)执行此操作的循环时遇到麻烦。 这是我到目前为止的代码:

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)

如果将上述代码中的[x]替换为一个人的ID#(例如00),该代码将运行并为我提供该人想要的数据框。 关于如何执行此操作的任何建议,以便我可以完成全部60个人的工作?

谢谢!

编辑因此,使用library(jsonlite)而不是library(rjson)以我需要的格式设置文件,而无需执行所有操作。 感谢所有人的答复,但是解决方案显然比我想象的要容易得多。

我不知道您的json文件的结构。 如果您不在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]])
}

在这里,您通常会读取该文件夹中的所有json文件,并构建一个长度为60的向量。然后,沿着该向量排序并读取所有文件。 我假设最后可以执行bind_rows或在for循环中添加代码。 但是请记住在循环开始之前将数据帧设置为NULL ,例如pam_long_pics <- NULL

希望有帮助吗? 让我知道。

遵循以下思路可以起作用:

#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)

编辑 :在读取JSON文件后,您可能需要也可能不需要包含as_tibble() ,具体取决于您的数据外观。

暂无
暂无

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

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