我在R中有一个数据框,对于前
Col1 Col2 Col3 Col4
Male 12345 76 abc
Female 545454 105 def
如何将数据转换为csv格式(只有cols和rows,没有元数据),然后转换为需要转换为base64编码字符串并传递给休息端点的字节?
此示例的base64编码字符串应为
Q29sMSAgLCAgQ29sMiAgICwgQ29sMyAgLCBDb2w0ICAgICAgICAgICAgICANCk1hbGUgICwgMTIzNDUgICAsNzYgICAgICwgYWJjICAgICAgICAgDQpGZW1hbGUsIDU0NTQ1NCAgLDEwNSAgICAsIGRlZg==
解码base64字符串后的数据如下所示: -
Col1 , Col2 , Col3 , Col4
Male , 12345 ,76 , abc
Female, 545454 ,105 , def
我尝试使用序列化,但序列化包括元数据在内的所有内容。我也试过writeBin,但这只适用于矢量
有什么想法吗?
答案 0 :(得分:1)
您是否检查了RCurl
功能?
df <- read.table(textConnection("Col1 Col2 Col3 Col4
Male 12345 76 abc
Female 545454 105 def"), header=T)
header_row <- paste(names(df), collapse=", ")
tab <- apply(df, 1, function(x)paste(x, collapse=", "))
text <- paste(c(header_row, tab), collapse="\n")
> text
[1] "Col1, Col2, Col3, Col4\nMale, 12345, 76, abc\nFemale, 545454, 105, def"
x <- base64(text)
> x
[1] "Q29sMSwgQ29sMiwgQ29sMywgQ29sNApNYWxlLCAgMTIzNDUsICA3NiwgYWJjCkZlbWFsZSwgNTQ1NDU0LCAxMDUsIGRlZg=="
attr(,"class")
[1] "base64"
> base64Decode(x)
[1] "Col1, Col2, Col3, Col4\nMale, 12345, 76, abc\nFemale, 545454, 105, def"