是否可以在R中以JSON格式将表写入文件? [英] Is it possible to write a table to a file in JSON format in R?

查看:142
本文介绍了是否可以在R中以JSON格式将表写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用R制作词频表,首选的输出格式是JSON文件.像 { "word":"dog", 频率":12 } 有什么方法可以将表直接保存为这种格式?我一直在使用write.csv()函数并将输出转换为JSON,但这非常复杂且耗时.

I'm making word frequency tables with R and the preferred output format would be a JSON file. sth like { "word" : "dog", "frequency" : 12 } Is there any way to save the table directly into this format? I've been using the write.csv() function and convert the output into JSON but this is very complicated and time consuming.

推荐答案

set.seed(1)
( tbl <- table(round(runif(100, 1, 5))) )

## 1  2  3  4  5 
## 9 24 30 23 14 

library(rjson)
sink("json.txt")
cat(toJSON(tbl))
sink()

file.show("json.txt")
## {"1":9,"2":24,"3":30,"4":23,"5":14}

甚至更好:

set.seed(1)
( tab <- table(letters[round(runif(100, 1, 26))]) )

a b c d e f g h i j k l m n o p q r s t u v w x y z 
1 2 4 3 2 5 4 3 5 3 9 4 7 2 2 2 5 5 5 6 5 3 7 3 2 1 

sink("lets.txt")
cat(toJSON(tab))
sink()
file.show("lets.txt")
## {"a":1,"b":2,"c":4,"d":3,"e":2,"f":5,"g":4,"h":3,"i":5,"j":3,"k":9,"l":4,"m":7,"n":2,"o":2,"p":2,"q":5,"r":5,"s":5,"t":6,"u":5,"v":3,"w":7,"x":3,"y":2,"z":1}

然后使用 http://www.jsonlint.com/进行验证,以获取漂亮的格式.如果您有多维表,则必须对其进行一些处理...

Then validate it with http://www.jsonlint.com/ to get pretty formatting. If you have multidimensional table, you'll have to work it out a bit...

哦,现在我明白了,您希望将数据集特征下沉到JSON文件中.没问题,只给我们一个示例数据,我会写一点代码.实际上,您需要将数据转换为所需的格式,然后将其转换为JSON. list应该足够.给我一点时间,我将更新我的答案.

Oh, now I see, you want the dataset characteristics sink-ed to a JSON file. No problem, just give us a sample data, and I'll work on a code a bit. Practically, you need to carry out the data into desirable format, hence convert it to JSON. list should suffice. Give me a sec, I'll update my answer.

编辑#2: 好吧,时间是相对的...这是一个常识...在这里,您可以进行以下操作:

EDIT #2: Well, time is relative... it's a common knowledge... Here you go:

( dtf <- structure(list(word = structure(1:3, .Label = c("cat", "dog", 
"mouse"), class = "factor"), frequency = c(12, 32, 18)), .Names = c("word", 
"frequency"), row.names = c(NA, -3L), class = "data.frame") )

##   word frequency
## 1   cat        12
## 2   dog        32
## 3 mouse        18

如果dtf是一个简单的数据帧,是的,data.frame,如果不是,则强制使用!长话短说,您可以这样做:

If dtf is a simple data frame, yes, data.frame, if it's not, coerce it! Long story short, you can do:

toJSON(as.data.frame(t(dtf)))
## [1] "{\"V1\":{\"word\":\"cat\",\"frequency\":\"12\"},\"V2\":{\"word\":\"dog\",\"frequency\":\"32\"},\"V3\":{\"word\":\"mouse\",\"frequency\":\"18\"}}"

尽管我需要一些melt,但简单的t可以解决问题.现在,只需要在转置data.frame之后处理列名. t将data.frames强制转换为矩阵,因此您需要将其转换回data.frame.我使用了as.data.frame,但是您也可以使用toJSON(data.frame(t(dtf)))-您将获得 X 而不是 V 作为变量名.另外,您可以使用regexp清理JSON文件(如果需要),但这是一种糟糕的做法,请尝试通过准备data.frame来解决该问题.

I though I'll need some melt with this one, but simple t did the trick. Now, you only need to deal with column names after transposing the data.frame. t coerces data.frames to matrix, so you need to convert it back to data.frame. I used as.data.frame, but you can also use toJSON(data.frame(t(dtf))) - you'll get X instead of V as a variable name. Alternatively, you can use regexp to clean the JSON file (if needed), but it's a lousy practice, try to work it out by preparing the data.frame.

我希望这会有所帮助...

I hope this helped a bit...

这篇关于是否可以在R中以JSON格式将表写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆