【问题标题】:Add quotation at the start and end of each line in R [closed]在R中每行的开头和结尾添加引号[关闭]
【发布时间】:2016-02-05 05:23:44
【问题描述】:

很抱歉我的问题不清楚,所以稍微改变了问题。

我正在用R对专利信息的数据进行预处理。部分数据包括申请人或公司名称如下:

head(patent$applicant)
[1] Flextronics AP, LLC                 Flextronics AP, LLC                 Flextronics AP, LLC                
[4] General Electric Company 

如何在R中每行的开头和结尾插入双引号?我打算的结果如下:

head(patent$applicant)
[1] "Flextronics AP, LLC"                 "Flextronics AP, LLC"                 "Flextronics AP, LLC"                
[4] "General Electric Company" 

希望 dput() 帮助你理解我的问题

head(dput(patent$applicant))
.......
"Zonar Systems, Inc.", "ZONAR SYSTEMS, INC.", "Zoom Information Systems", 
"Zoop Mobility Network Inc.", "Zynga Inc."), class = "factor")
[1] Flextronics AP, LLC                 Flextronics AP, LLC                        Flextronics AP, LLC                
[4] General Electric Company            GM Global Technology Operations LLC     Digimarc Corporation               
2994 Levels:  @Road, Inc. 1st Communications Inc. 2089275 ONTARIO LTD. ...    Zynga Inc.

还有 str()。

str(patent$applicant)
Factor w/ 2994 levels "","@Road, Inc.",..: 864 864 864 956 1007 667 1512 2510 1671 1671 ...

我试过 paste0()。结果如下:

head(paste0('"', patent$applicant, '"'))
[1] "\"Flextronics AP, LLC\""                 "\"Flextronics AP, LLC\""                
[3] "\"Flextronics AP, LLC\""                 "\"General Electric Company\""           
[5] "\"GM Global Technology Operations LLC\"" "\"Digimarc Corporation\""

感谢 cmets。它真的帮助了我。 我用单引号而不是双引号来做到这一点。

> head(paste0("\'",patent$applicant,"\'"))
[1] "'Flextronics AP, LLC'"                 "'Flextronics AP, LLC'"                
[3] "'Flextronics AP, LLC'"                 "'General Electric Company'"           
[5] "'GM Global Technology Operations LLC'" "'Digimarc Corporation'"   

【问题讨论】:

  • 你的这个“列表”是什么 - 一个文本文件?
  • @Stedy - R 不会自动添加引号,它会在某些情况下将print() 带引号到屏幕上,但基本数据不会改变。
  • 对不起,我重新写了问题。
  • 请使用dput显示数据。它将帮助我们理解结构。
  • 您有一个factor,它表示group1group2 等分类数据。这是附有文本标签的数字数据。如果您希望这是严格的字符数据,请使用上面的 as.character 代码。

标签: r string text


【解决方案1】:

如果数据是字符串格式,你可以使用paste()函数有一个前导和一个尾随双引号。

【讨论】:

  • 虽然提问者发现这个答案很有用并被标记为想要的答案,但为什么人们会否决它?我是新人,所以如果有什么我不知道的,请告诉我。
  • 因为 at is,它是一个非常糟糕的解决方案,并且提供的有用信息比上面的 cmets 少(即使是 OP 也同意)。您可以为此答案添加更多价值,提供如何使用您提出的解决方案的示例。因此,我将重新考虑我的否决票。
猜你喜欢
  • 1970-01-01
  • 2012-02-09
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
相关资源
最近更新 更多