替换","用" "" "在R字符串中

时间:2015-07-07 22:19:21

标签: r string replace

我有一个R字符串

y <- "age,income,homeowner_status_desc,marital_status_cd,gender,education"

我想将其转换为:

y ="age","income","homeowner_status_desc","marital_status_cd","gender","education"

所以,基本上我想用,替换字符串中的","。我尝试使用gsub(",",'","' , y),但它没有提供我正在寻找的输出。

我怎样才能在R里面做到?

2 个答案:

答案 0 :(得分:2)

只需使用cat功能显示输出。

> y <- "age,income,homeowner_status_desc,marital_status_cd,gender,education"
> gsub(",", "\",\"", y)
[1] "age\",\"income\",\"homeowner_status_desc\",\"marital_status_cd\",\"gender\",\"education"
> x <- gsub(",", "\",\"", y)
> cat(x)
age","income","homeowner_status_desc","marital_status_cd","gender","education

答案 1 :(得分:1)

这是你想要的吗?

> (y <- unlist(strsplit(y, ",")))
[1] "age"                   "income"                "homeowner_status_desc" "marital_status_cd"    
[5] "gender"                "education"   

约翰