如何从r中的列标题中删除_字符(0)_字符(0)

发布于 2025-02-10 21:26:20 字数 645 浏览 1 评论 0原文

我正在使用R中的DF工作,并且列标题也正在获取_ contric> _ trace(0)_ trace(0)值。我想用“ na”或“”替换/删除它们,以便我可以 trimws()删除空间。我该怎么做?

|S.no|A _ character(0) _ character(0)|B _character(0) _ character(0)| 
|:---|:-----------------------------|:----------------------------|
|1   | 20                           |55                           |
|2   | 30                           |56                           |

我尝试了colnames(df)< - gsub(“ _ artial(0)_ targinal(0)”,“”,colnames(df)),以便我以后可以修剪空间以获取列标题为,

|S.no|A|B|
|:---|:|:|

但我仍然在标题中获得相同的字符。

I'm working on a df in R and the column headers are getting _ character(0) _ character(0) values too. I want to replace/remove them either with "NA" or "" so that I can trimws() to remove the spaces. How would I do that?

|S.no|A _ character(0) _ character(0)|B _character(0) _ character(0)| 
|:---|:-----------------------------|:----------------------------|
|1   | 20                           |55                           |
|2   | 30                           |56                           |

I tried colnames(df) <- gsub(" _ character(0)_ character(0)","",colnames(df)) so that I can trim the space later to just get the column header as

|S.no|A|B|
|:---|:|:|

But I'm still getting the same characters in the header.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-02-17 21:26:20

您将需要逃脱此字符才能使其起作用:

df <- data.frame(`A _ character(0)_ character(0)` = 1, 
                 check.names = F)
df
  A _ character(0)_ character(0)
1                              1
colnames(df) <- gsub(" _ character\\(0)_ character\\(0)", "", colnames(df))

df
  A
1 1

You will need to escape this character to make it work: (

df <- data.frame(`A _ character(0)_ character(0)` = 1, 
                 check.names = F)
df
  A _ character(0)_ character(0)
1                              1
colnames(df) <- gsub(" _ character\\(0)_ character\\(0)", "", colnames(df))

df
  A
1 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文