首页 > 解决方案 > 从 R 中的列创建循环以进行代码替换

问题描述

我想复制并粘贴几次,但不是示例中包含的“代码”,而是包含存储在 xlsx 数据集的列上的值。这是我的起点(R 脚本):

r <- POST(paste(url_base,service,method,sep="/"),

body = paste("{'entity':'sigpac', code: 'imported_file$code' }",sep=""),

add_headers(.headers = c("Content-Type"="application/json","authorization"=token)))

因此,在“imported_file$code”中,我想定义循环以重复除列的不同行之外的所有内容。

例如,这将是一个可悲的结果。

[{
            "entity":"sigpac",
            "code":"18188-7-81-11-1",
            "status":"ON"
    
    },{
    "entity":"sigpac",
    "code":"18142-20-237-0-1",
    "status":"ON"


},{
        "entity":"sigpac",
        "code":"181232-20-237-0-1",
        "status":"ON"}
},{
            "entity":"sigpac",
            "code":"1315432-20-237-0-1",
            "status":"ON"}

(ETC...)

如您所见,只有参数code是我要更改的参数。

为了澄清,我重新提出了这个问题。感谢我学习的帮助!

标签: r

解决方案


这个怎么样?

imported_file <- data.frame(code = c("18188-7-81-11-1", 
                                     "18142-20-237-0-1", 
                                     "181232-20-237-0-1",
                                     "1315432-20-237-0-1"))

mystring <- paste('{\n\t"entity":"sigpac",\n\t"code":"', imported_file$code, '",\n\t"status":"ON"\n}',sep="",collapse=",")

cat(mystring)

推荐阅读