gpt4 book ai didi

r - 解析回 'messy' API 结构

转载 作者:行者123 更新时间:2023-12-04 09:45:21 27 4
gpt4 key购买 nike

我正在通过 API 从在线数据库 (REDCap) 获取数据,数据以逗号分隔的字符串形式传递,如下所示,

RAW.API <- structure("id,event_arm,name,dob,pushed_text,pushed_calc,complete\n\"01\",\"event_1_arm_1\",\"John\",\"1979-05-01\",\"\",\"\",2\n\"01\",\"event_2_arm_1\",\"John\",\"2012-09-02\",\"abc\",\"123\",1\n\"01\",\"event_3_arm_1\",\"John\",\"2012-09-10\",\"\",\"\",2\n\"02\",\"event_1_arm_1\",\"Mary\",\"1951-09-10\",\"def\",\"456\",2\n\"02\",\"event_2_arm_1\",\"Mary\",\"1978-09-12\",\"\",\"\",2\n", "`Content-Type`" = structure(c("text/html", "utf-8"), .Names = c("", "charset")))

我有这个脚本可以很好地将它解析成数据框,

(df <- read.table(file = textConnection(RAW.API), header = TRUE, 
sep = ",", na.strings = "", stringsAsFactors = FALSE))
id event_arm name dob pushed_text pushed_calc complete
1 1 event_1_arm_1 John 1979-05-01 <NA> NA 2
2 1 event_2_arm_1 John 2012-09-02 abc 123 1
3 1 event_3_arm_1 John 2012-09-10 <NA> NA 2
4 2 event_1_arm_1 Mary 1951-09-10 def 456 2
5 2 event_2_arm_1 Mary 1978-09-12 <NA> NA 2

然后我做了一些计算并将它们写入 pushed_textpushed_calc 然后我需要将数据格式化回它进来的困惑的逗号分隔结构。

我想象的是这样的,

API.back <- `some magic command`(df, ...)

identical(RAW.API, API.back)
[1] TRUE

一些命令可以从我制作的数据框中格式化我的数据,df,回到原始 API 对象进入的结构,RAW.API

如有任何帮助,我们将不胜感激。

最佳答案

这似乎可行:

some_magic <- function(df) {
## Replace NA with "", converting column types as needed
df[] <- lapply(df, function(X) {
if(any(is.na(X))) {X[is.na(X)] <- ""; X} else {X}
})

## Print integers in first column as 2-digit character strings
## (DO NOTE: Hardwiring the number of printed digits here is probably
## inadvisable, though needed to _exactly_ reconstitute RAW.API.)
df[[1]] <- sprintf("%02.0f", df[[1]])

## Separately build header and table body, then suture them together
l1 <- paste(names(df), collapse=",")
l2 <- capture.output(write.table(df, sep=",", col.names=FALSE,
row.names=FALSE))
out <- paste0(c(l1, l2, ""), collapse="\n")

## Reattach attributes
att <- list("`Content-Type`" = structure(c("text/html", "utf-8"),
.Names = c("", "charset")))
attributes(out) <- att
out
}

identical(some_magic(df), RAW.API)
# [1] TRUE

关于r - 解析回 'messy' API 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12393004/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com