我是R的初学者。
我有一个csv文件:
key1 key2 key3 key4 key5 key6
value value value value value
value value value value value
value value value value value
etc.
我将CSV文件转换为这种格式,然后将其保存为.txt文件,这样我就可以在批量上传中将其用于需要这种格式的服务器:
{"key1": "value", "key2": "value", "key3": "value", "key4": "value", "key5": "value"}
{"key1": "value", "key2": "value", "key3": "value", "key4": "value", "key5": "value"}
{"key1": "value", "key2": "value", "key3": "value", "key4": "value", "key5": "value"}
这是我到目前为止在Rstudio中拥有的内容:
#install packages
install.packages('jsonlite')
#libraries
library(readr)
library(jsonlite)
#import data
plantasia_menu_items <- read_csv("plantasia - menu_items.csv")
#View(plantasia_menu_items)
#to Json format
inJSON <- toJSON(plantasia_menu_items)
table <- fromJSON(inJSON)
print(inJSON)
这会产生以下格式的数据,但是一旦将其包含在JSON中然后保存,我就不知道如何重构它:
{"key1": "value", "key2": "value", "key3": "value", "key4": "value", "key5": "value"} {"key1": "value", "key2": "value", "key3": "value", "key4": "value", "key5": "value"} {"key1": "value", "key2": "value", "key3": "value", "key4": "value", "key5": "value"}
服务器必须在新行上才能了解批量上传并创建新条目。现在卡住了一段时间。关于如何将其重组为矩阵的任何想法?
答案 0 :(得分:0)
这是我最终用来重新格式化数据的代码。 但是,我不确定如何将其保存到.txt文件。我无法将其放入数据框,因为它不是矢量。有任何想法吗?
#clean the working environment
rm(list=ls())
#set working directory, keep files organized
setwd("C:/Users/")
#
#install packages('readr'}
#install.packages('jsonlite')
#install.packages("stringr")
#install.packages("stringi")
#libraries
library(readr)
library(jsonlite)
library(stringr)
library(stringi)
#import data in csv format
data <- read_csv("test.csv")
#View(data)
#Convert the CSV to Json format and verify it worked by printing
inJSON <- toJSON(data)
print(inJSON)
#Finding the locations of the separators of each data entry.
#This will allow us to determine to know where to begin and end the substring
# generating two tables indicating each location of patterns along the whole string
start_table <- data.frame(str_locate_all(inJSON, "key1"))
#start_table
stop_table <- data.frame(str_locate_all(inJSON, ","))
#selecting row 1, col 2.
#stop_table[1,2]
#defining the function that will restructure the data for bulk upload format
restrucJSON <- function(jsonformat){
#prepping the loop
count <- str_count(jsonformat, "key1") #count the number of times the loop is going to run, or the number of database entries
a <- 1 #there is only one row per entry
b <- 6 #there are 6 columns in this data, so we take every 6th position of the commas
#loop to print
for (i in c(1:count)) { #using the count to tell the loop how many times to run
x <- start_table[a,1]
y <- stop_table[b,1]
print(substr(jsonformat, x-2, y-1))
a <- a+1 #
b <- b+6 #start counting from the next 6th comma
}
}
#try out the function with the inJSON object
export <- restrucJSON(inJSON)