R: convert variable to csv string

前端 未结 2 823
北海茫月
北海茫月 2021-01-19 15:56

I would like to know how to convert a variable in R to csv string.

> x = c(1:10)
> a = write.csv(x)
\"\",\"x\"
\"1\",1
\"2\",2
\"3\",3
\"4\",4
\"5\",5         


        
2条回答
  •  攒了一身酷
    2021-01-19 16:01

    Here's an alternative, even easier:

    foo2 <- capture.output(write.csv(matrix(1:6,nrow=2), stdout(), row.names=F)) 
    foo2
    ## "\"V1\",\"V2\",\"V3\"" "1,3,5"                "2,4,6" 
    

    Probably what you want is:

    foo2[-1,]    
    ## "\"V1\",\"V2\",\"V3\"" "1,3,5"                "2,4,6" 
    

提交回复
热议问题