使用 json 文件根据 R 中的变量删除某些数据行块
Removing certain chunk of data lines based on a variable in R with json file
我一直在使用 R 中的 json 格式文件。我试图根据变量摆脱不必要的数据行。例如,我试图在
时删除整行
"state: incorrect"
事件发生在以下几行:
[
{
"id": "id1",
"state": "correct",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
},
"time": "2018-04-15T23:58:15.888700+00:00"
},
{
"id": "id1",
"state": "incorrect",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:06.344300+00:00"
},
{
"id": "28s1",
"state": :"incorrect",
"object": ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:05.261600+00:00"
}
]
我想要这样的东西:
[
{
"id": "id1",
"state": "correct",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
},
"timeStamp": "2018-04-15T23:58:15.888700+00:00"
}
]
如果有任何评论、帮助和建议,我将不胜感激。
您的 JSON 字符串中有一些拼写错误,但在编辑它们之后:
library(jsonlite)
df <- fromJSON(json_str )
df
# id state object http://54&46;210&46;82&46;207/lock/public/ext/gam/type time
# 1 id1 correct ball c4 2018-04-15T23:58:15.888700+00:00
# 2 id1 incorrect ball c5 2018-04-15T23:58:06.344300+00:00
# 3 28s1 incorrect ball c5 2018-04-15T23:58:05.261600+00:00
df <- subset(df,state!="incorrect")
toJSON(df,pretty = TRUE)
# [
# {
# "id": "id1",
# "state": "correct",
# "object": "ball",
# "data": {
# "http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
# },
# "time": "2018-04-15T23:58:15.888700+00:00"
# }
# ]
数据
json_str <-
'[
{
"id": "id1",
"state": "correct",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
},
"time": "2018-04-15T23:58:15.888700+00:00"
},
{
"id": "id1",
"state": "incorrect",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:06.344300+00:00"
},
{
"id": "28s1",
"state": "incorrect",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:05.261600+00:00"
}
]'
我一直在使用 R 中的 json 格式文件。我试图根据变量摆脱不必要的数据行。例如,我试图在
时删除整行"state: incorrect"
事件发生在以下几行:
[
{
"id": "id1",
"state": "correct",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
},
"time": "2018-04-15T23:58:15.888700+00:00"
},
{
"id": "id1",
"state": "incorrect",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:06.344300+00:00"
},
{
"id": "28s1",
"state": :"incorrect",
"object": ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:05.261600+00:00"
}
]
我想要这样的东西:
[
{
"id": "id1",
"state": "correct",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
},
"timeStamp": "2018-04-15T23:58:15.888700+00:00"
}
]
如果有任何评论、帮助和建议,我将不胜感激。
您的 JSON 字符串中有一些拼写错误,但在编辑它们之后:
library(jsonlite)
df <- fromJSON(json_str )
df
# id state object http://54&46;210&46;82&46;207/lock/public/ext/gam/type time
# 1 id1 correct ball c4 2018-04-15T23:58:15.888700+00:00
# 2 id1 incorrect ball c5 2018-04-15T23:58:06.344300+00:00
# 3 28s1 incorrect ball c5 2018-04-15T23:58:05.261600+00:00
df <- subset(df,state!="incorrect")
toJSON(df,pretty = TRUE)
# [
# {
# "id": "id1",
# "state": "correct",
# "object": "ball",
# "data": {
# "http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
# },
# "time": "2018-04-15T23:58:15.888700+00:00"
# }
# ]
数据
json_str <-
'[
{
"id": "id1",
"state": "correct",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c4"
},
"time": "2018-04-15T23:58:15.888700+00:00"
},
{
"id": "id1",
"state": "incorrect",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:06.344300+00:00"
},
{
"id": "28s1",
"state": "incorrect",
"object": "ball",
"data": {
"http://54&46;210&46;82&46;207/lock/public/ext/gam/type": "c5"
},
"time": "2018-04-15T23:58:05.261600+00:00"
}
]'