从列名中删除引号? [英] remove quotes from colnames?

查看:54
本文介绍了从列名中删除引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式的数据框

<头>
column1"column2"
15
26
37

如何去掉列名中的引号?我试过使用 gsub 但我不能引用引号哈哈.还需要一种方法来做到这一点,而不仅仅是 names(data) <- c(column1", column2").谢谢大家!

How do I remove the quotation mark from the column names? I've tried using gsub but I can't quote quotation marks haha. Also need a way to do this that isn't just names(data) <- c("column1", "column2"). Thank you all!

推荐答案

您可以将 gsub 与单引号一起使用,以便引用双引号字符进行替换:

You can use gsub with single-quotes in order to reference the double-quote character for replacement:

names(df) = gsub('"', "", names(df))

测试:

# Set up data
d = mtcars[1:3, 1:4]
names(d)[1:2] = c('"column1"', '"column2"')

names(d)
#> [1] "\"column1\"" "\"column2\"" "disp"        "hp"

d
#>               "column1" "column2" disp  hp
#> Mazda RX4          21.0         6  160 110
#> Mazda RX4 Wag      21.0         6  160 110
#> Datsun 710         22.8         4  108  93

# Remove quotation marks from column names
names(d) = gsub('"', "", names(d))
names(d)
#> [1] "column1" "column2" "disp"    "hp"

d
#>               column1 column2 disp  hp
#> Mazda RX4        21.0       6  160 110
#> Mazda RX4 Wag    21.0       6  160 110
#> Datsun 710       22.8       4  108  93

reprex 包 (v0.3.0) 于 2021 年 1 月 19 日创建

Created on 2021-01-19 by the reprex package (v0.3.0)

这篇关于从列名中删除引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆