我有一个名为“wheat_cities”的数据框
我的dataframe中的列如下
"Date" "Wheat..Maximum.Price"
[3] "Wheat..Minimum.Price" "Wheat..Modal.Price"
[5] "Wheat..North.Zone..Agra" "Wheat..North.Zone..Amritsar"
[7] "Wheat..North.Zone..Bhatinda" "Wheat..North.Zone..Chandigarh"
[9] "Wheat..North.Zone..Dehradun" "Wheat..North.Zone..Delhi"
[11] "Wheat..North.Zone..Gurgaon" "Wheat..North.Zone..Haldwani"
[13] "Wheat..North.Zone..Hisar" "Wheat..North.Zone..Jammu"
[15] "Wheat..North.Zone..Kanpur" "Wheat..North.Zone..Karnal"
[17] "Wheat..North.Zone..Lucknow" "Wheat..North.Zone..Ludhiana"
[19] "Wheat..North.Zone..Mandi" "Wheat..North.Zone..Panchkula"
[21] "Wheat..North.Zone..Shimla" "Wheat..North.Zone..Srinagar"
[23] "Wheat..North.Zone..Varanasi" "Wheat..West.Zone..Ahmedabad"
[25] "Wheat..West.Zone..Bhopal" "Wheat..West.Zone..Bhuj"
[27] "Wheat..West.Zone..Gwalior" "Wheat..West.Zone..Indore"
[29] "Wheat..West.Zone..Jabalpur" "Wheat..West.Zone..Jaipur"
[31] "Wheat..West.Zone..Jodhpur" "Wheat..West.Zone..Kota"
[33] "Wheat..West.Zone..Mumbai" "Wheat..West.Zone..Nagpur"
[35] "Wheat..West.Zone..Panaji" "Wheat..West.Zone..Raipur"
[37] "Wheat..West.Zone..Rajkot" "Wheat..West.Zone..Rewa"
[39] "Wheat..West.Zone..Sagar" "Wheat..West.Zone..Surat"
[41] "Wheat..East.Zone..Bhagalpur" "Wheat..East.Zone..Bhubaneshwar"
[43] "Wheat..East.Zone..Cuttack" "Wheat..East.Zone..Patna"
[45] "Wheat..East.Zone..Purnia" "Wheat..East.Zone..Ranchi"
[47] "Wheat..East.Zone..Rourkela" "Wheat..East.Zone..Sambalpur"
[49] "Wheat..East.Zone..Siliguri" "Wheat..North.East.Zone..Aizwal"
[51] "Wheat..North.East.Zone..Dimapur" "Wheat..North.East.Zone..Guwahati"
[53] "Wheat..North.East.Zone..Itanagar" "Wheat..North.East.Zone..Shillong"
[55] "Wheat..South.Zone..Bengaluru" "Wheat..South.Zone..Chennai"
[57] "Wheat..South.Zone..Coimbatore" "Wheat..South.Zone..Dharwad"
[59] "Wheat..South.Zone..Dindigul" "Wheat..South.Zone..Ernakulam"
[61] "Wheat..South.Zone..Hyderabad" "Wheat..South.Zone..Karimnagar"
[63] "Wheat..South.Zone..Kozhikode" "Wheat..South.Zone..Mangalore"
[65] "Wheat..South.Zone..Mysore" "Wheat..South.Zone..Palakkad"
[67] "Wheat..South.Zone..Port.Blair" "Wheat..South.Zone..Puducherry"
[69] "Wheat..South.Zone..Thiruchirapalli" "Wheat..South.Zone..Thiruvananthapuram"
[71] "Wheat..South.Zone..Thrissur" "Wheat..South.Zone..Tirunelveli"
[73] "Wheat..South.Zone..Vijaywada" "Wheat..South.Zone..Visakhapatnam"
[75] "Wheat..South.Zone..Warangal" "Wheat..South.Zone..Wayanad"
>
我想更改列名,以便对于第 5-76 列,我只在第二个“..”之后得到名称。对于第 2 列和第 3 列,我在第一个“..”之后得到名称
由于字符长度不同,我无法使用子字符串命令。
请帮忙。提前致谢!
最佳答案
我们可以使用 sub
来匹配字符 (.*
) 后跟两个点 (\\.{2}
),捕获一组 ((.*)
) 之后的字符,直到字符串的末尾 ($
) 并替换为反向引用 (\\1
) 捕获的组
names(data) <- sub(".*\\.{2}(.*)$", "\\1", names(data))
names(data)
#[1] "Date" "Maximum.Price" "Minimum.Price" "Agra"
数据
data <- data.frame(Date = c("2013-01-01", "2013-01-02"),
Wheat..Maximum.Price = 5:6, Wheat..Minimum.Price = 1:2,
Wheat..North.Zone..Agra = 6:7, stringsAsFactors = FALSE)
关于r - 在 R 中更改多个列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421578/