简体   繁体   English

在 rStudio 中重命名多个列的代码

[英]Code to rename multiple columns in rStudio

I want to rename this columns in R, I want to remove X from each of them so that it remains just figures which represents different years varying from 1960 to 2020. The first two (country name and Country Code) are sorted out already.我想在 R 中重命名这些列,我想从每个列中删除X ,以便它只保留代表从 1960 年到 2020 年不同年份的数字。前两个(国家名称和国家代码)已经排序。

[1] "ï..Country.Name" "Country.Code"    "X1960"           "X1961"           "X1962"          
 [6] "X1963"           "X1964"           "X1965"           "X1966"           "X1967"          
[11] "X1968"           "X1969"           "X1970"           "X1971"           "X1972"          
[16] "X1973"           "X1974"           "X1975"           "X1976"           "X1977"          
[21] "X1978"           "X1979"           "X1980"           "X1981"           "X1982"          
[26] "X1983"           "X1984"           "X1985"           "X1986"           "X1987"          
[31] "X1988"           "X1989"           "X1990"           "X1991"           "X1992"          
[36] "X1993"           "X1994"           "X1995"           "X1996"           "X1997"          
[41] "X1998"           "X1999"           "X2000"           "X2001"           "X2002"          
[46] "X2003"           "X2004"           "X2005"           "X2006"           "X2007"          
[51] "X2008"           "X2009"           "X2010"           "X2011"           "X2012"          
[56] "X2013"           "X2014"           "X2015"           "X2016"           "X2017"          
[61] "X2018"           "X2019"           "X2020" 
names(df) <- gsub("^X", "", names(df))

gsub() matches a regular expression and replaces it if found. gsub() 匹配正则表达式并在找到时替换它。 In our case, the regex says the string must have an X at the beginning.在我们的例子中,正则表达式表示字符串必须以 X 开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM