我当时正在通过Android应用收集数据,这些数据已保存到Firebase实时数据库中。这是数据图片> dataScreen
此应用是游戏,我在其中收集有关玩家,年龄,性别等的基本信息。 然后,游戏活动开始。随机对象显示在移动设备的屏幕上,玩家必须对此对象做出反应,如果他们做出反应,我会将他们的反应(如x,y,时间(以毫秒为单位))保存到对象中,并将其名称保存在firebase中 进入水平反应。如您在图片中所看到的,如果我下载了它的列表,我尝试了tidyjson包,但它无法使用列表...
我需要一些解决方案,以这种方式将我的数据转换为数据框或数据表
昵称,年龄,性别,..基本信息..,x,y,时间(这是一行)
我需要为每个玩家记录每个反应的行(36行,其中一个玩家有36个反应)
任何解决方案/建议?
library(tidyjson)
library(RCurl)
raw_data <- getURL("https://myfirebase.firebaseio.com/.json")
data <- fromJSON(raw_data)
dput(head(data,2))
Qwerty = list(
age = "50",
education = "nič",
gender = "Muz",
glasses = "Mam okuliare",
nick = "Qwerty",
psc = "08005",
reactions = list(
list(time = 584, x = 814,y = 1615),
list(time = 743, x = 935, y = 1366),
list(time = 463, x = 1009, y = 1230),
list(time = 485, x = 733, y = 1488),
list(time = 489, x = 482, y = 1433),
list(time = 483, x = 952, y = 601),
list(time = 506, x = 20, y = 430),
list(time = 430, x = 942, y = 871),
list(time = 495, x = 785, y = 84),
list(time = 417, x = 769, y = 1682),
list(time = 433, x = 334, y = 860),
list(time = 439, x = 979, y = 1183),
list(time = 458, x = 211, y = 1146),
list(time = 537, x = 134, y = 1682),
list(time = 472, x = 812, y = 1655),
list(time = 448, x = 870, y = 1539),
list(time = 447, x = 722, y = 910),
list(time = 477, x = 876, y = 937),
list(time = 448, x = 723, y = 942),
list(time = 1479, x = 989, y = 864),
list(time = 833, x = 1003, y = 1682),
list(time = 466, x = 738, y = 507),
list(time = 508, x = 627, y = 1664),
list(time = 490, x = 340, y = 1070),
list(time = 874, x = 490, y = 434),
list(time = 469, x = 423, y = 369),
list(time = 482, x = 107, y = 1075),
list(time = 453, x = 102, y = 1341),
list(time = 491, x = 473, y = 1201),
list(time = 479,x = 210, y = 517),
list(time = 533, x = 902, y = 979),
list(time = 487, x = 1009, y = 1161),
list(time = 446, x = 823, y = 608),
list(time = 478, x = 279, y = 35),
list(time = 482, x = 163, y = 1238),
list(time = 453, x = 667, y = 697)
),
score = 532),
#this is next player object
Qwertz123 = list(
age = "22", education = "SS", gender = "Muz", glasses = "Nemam okuliare",
nick = "Qwertz123", psc = "04013", reactions = list(list(
time = -1, x = 557, y = 89), list(time = -1, x = 50,
y = 648), list(time = 220, x = 47, y = 422), list(time = -1,
x = 464, y = 1476), list(time = 694, x = 846, y = 1375),
list(time = 618, x = 1009, y = 1011), list(time = 503,
x = 45, y = 543), list(time = 574, x = 368, y = 1600),
list(time = -1, x = 24, y = 1605), list(time = 549, x = 451,
y = 1063), list(time = 857, x = 602, y = 1047), list(
time = 451, x = 659, y = 1228), list(time = 556,
x = 655, y = 1682), list(time = 444, x = 723, y = 617),
list(time = -1, x = 1009, y = 1271), list(time = 18,
x = 811, y = 350), list(time = 1383, x = 14, y = 255),
list(time = -1, x = 575, y = 1337), list(time = 542,
x = 648, y = 76), list(time = 647, x = 964, y = 1600),
list(time = 458, x = 564, y = 177), list(time = 485,
x = 820, y = 1413), list(time = 1070, x = 359, y = 1288),
list(time = 677, x = 668, y = 1176), list(time = 637,
x = 718, y = 130), list(time = 476, x = 719, y = 1123),
list(time = 713, x = 312, y = 263), list(time = 473,
x = 122, y = 490), list(time = 395, x = 584, y = 1625),
list(time = 372, x = 871, y = 355), list(time = 411,
x = 495, y = 617), list(time = 449, x = 109, y = 1086),
list(time = 367, x = 400, y = 38), list(time = 449, x = 119,
y = 850)), score = 553)
然后我要使用tidyjson包,这只是示例
data %>% as.tbl_json %>% enter_object("reactions")
Error in UseMethod("as.tbl_json") :
no applicable method for 'as.tbl_json' applied to an object of class "list"
此软件包对于我的问题似乎很好,但不适用于列表。
我需要像此示例一样创建数据框
playersDataFrame <- data.frame(
age = "50",
education = "nič",
gender = "Muz",
glasses = "Mam okuliare",
nick = "Qwerty",
psc = "08005",
x = 23,
y = 32,
time = 443
)
此数据框代表玩家Qwerty创建的一排反应,如您所见,每个玩家有36次反应,目前玩家人数为61
我需要提取每个反应并将其与玩家基本数据排成一行
我已经保存了诸如对象之类的播放器,但是我需要像这样的示例那样保存数据
答案 0 :(得分:0)
我意识到我不需要使用json,如果它像csv一样保存,那么我会比遇到问题更容易阅读它。这绝对是一个随机的想法,但它有效:D
for(x in c(1:length(data))){
for(y in c(1:length(data[[x]]$reactions))){
write.table(data.frame(data[[x]]$age,data[[x]]$education,data[[x]]$gender,data[[x]]$glasses,data[[x]]$nick,data[[x]]$psc,
data[[x]]$reactions[[y]]$x,data[[x]]$reactions[[y]]$y,data[[x]]$reactions[[y]]$time),
file = "players.csv", sep = ",", append = TRUE, quote = FALSE,
col.names = FALSE, row.names = FALSE)
}
}
然后我只使用read.csv()