我有一个名为“wheat_cities”的数据框
我的数据框中的列如下
"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列,我在第一个“..”之后得到了名称
由于字符长度不同,我无法使用substring命令。
请帮忙。提前谢谢!
答案 0 :(得分:2)
我们可以使用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)
答案 1 :(得分:1)
names(data) <- gsub("Wheat..", "", names(data), fixed = T)
names(data) <- gsub("North.Zone..", "", names(data), fixed = T)
names(data)
# [1] "Date" "Maximum.Price" "Minimum.Price" "Modal.Price" "Agra" "Amritsar"
首先我们删除&#34;小麦..&#34;从所有列名称中删除&#34; North.Zone ..&#34;。
答案 2 :(得分:0)
您可以使用strsplit
,这允许您使用特定值拆分字符串,例如“..”。
正如你所说,从5到5,你需要在最后一个“..”之后的名字,从2到4你想要第三个名字在“..”之间,你可以用这个实例来做:
change_names <- strsplit(colnames(wheat_cities), '[..]')
for(i in 2 : ncol(wheat_cities)){
if(i %in% c(2 : 4)){
colnames(wheat_cities)[i] <- change_names[[i]][3]
}else{
colnames(wheat_cities)[i] <- last(change_names[[i]])
}
}