R - How to remove "c(" from list being written to file -
this 1 of things hard search on google...
i have dataframe:
x=c("a", "b", "c") y=list(c(1,2,3), c("f","g"), c(1,"r","$")) df = as.data.frame(x) df$y = y i tranform such , print .txt:
p = sprintf('{"name":"%s", "import": [%s]}', df$x, df$y) write(p, "p.txt") the output print c() notation:
{"name":"a", "import": [c(1, 2, 3)]} {"name":"b", "import": [c("f", "g")]} {"name":"c", "import": [c("1", "r", "$")]} how can remove prints like:
{"name":"a", "import": [1, 2, 3]} {"name":"b", "import": ["f", "g"]} {"name":"c", "import": ["1", "r", "$"]}
this looks straightforward, have use string substitution. example these:
x=c("a", "b", "c") y=list(c(1,2,3), c("f","g"), c(1,"r","$")) df = as.data.frame(x) df$y = y p = sprintf('{"name":"%s", "import": [%s]}', df$x, df$y) p <- gsub("\\[c\\("," \\[",p) p <- gsub("\\)\\]","\\]",p) write(p, "p.txt")
Comments
Post a Comment