将R data.frame转换为Javascript数组 [英] Convert R data.frame to Javascript array

查看:80
本文介绍了将R data.frame转换为Javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据框的某些列保存为特定格式(JavaScript格式).我试图从rjson包中使用toJSON(),但这样做无效.

I want to save some columns of my data frame into a specific format (JavaScript format).I'v tried to use toJSON() from rjson package but that deosen't works.

我的结果应类似于: http://leaflet.github.io/Leaflet.markercluster/example/realworld.388.js

请帮助,并预先感谢.

推荐答案

最快的方法是使用 apply 函数之一.碰巧,我发现最好的一个就是 apply 本身.

The fastest way to do it is going to use one of the apply functions. It just so happens the best one I found was apply itself.

# this will go row by row
apply(allTheData, 1, function(x){
    print(x["COL_NAME"])
})

您不能在申请中使用 x $ COL_NAME ,因此您必须使用上面的方式.

You can't use x$COL_NAME in apply so you have to use the way I did above.

您可以使用其他 apply 函数,但是要逐行查找,我发现这是最容易应用的函数.

You could use other apply functions, but to go row by row I found this one the easiest to apply.

这篇关于将R data.frame转换为Javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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