从R中的字符串中删除字符(Removing characters from string in R)

编程入门 行业动态 更新时间:2024-10-26 12:31:46
从R中的字符串中删除字符(Removing characters from string in R)

我在R输出中有一些制表符或空格符(我怀疑输出来自哪个任务中的问题),使其看起来像这样:

[1841] "\t\t\tGreen\n\t\t" [1842] "Blue" [1843] "\t\t\tRed\n\t\t"

对于一个同事,我必须将它读入SPSS,并且在将它作为txt数据读取时会遇到一些麻烦,所以我想删除字符串中的\ t和\ n部分:

str_replace(mydata, "([\n])", "")

试用\ n和\ t或组合,但从未完全奏效。

我的错误在哪里?

I have some tabs or space breaks in my R output (I suspect the problem in the task from which the output comes), making it look like this:

[1841] "\t\t\tGreen\n\t\t" [1842] "Blue" [1843] "\t\t\tRed\n\t\t"

For a colleague I have to read this into SPSS and that gives some trouble when reading this as txt data, so I wanted to remove the \t and \n parts in my string:

str_replace(mydata, "([\n])", "")

Tried it with both \n and \t or combinations, but never quite worked.

Where is my mistake?

最满意答案

您需要使用str_replace_all删除多个空白字符的帐户。 为什么不使用base R删除这些字符而不是加载stringr包?

gsub('[\t\n]', '', mydata)

You need to use str_replace_all to remove the multiple accounts of whitespace characters. Why not use base R to remove these characters instead of loading the stringr package?

gsub('[\t\n]', '', mydata)

更多推荐

本文发布于:2023-08-07 20:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   Removing   characters   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!