从带有r的列标题列名中删除中间不一致的字符



`套种子(500)

索引<-样本(1:nrow(Bands_reflectance_2017),100,替换=假)

波段_反射率_2017<-dput(头(Bands_reflectance_2017[1:10])

波段_反射率_2017&lt-结构(列表(t2017.01.05T08.25.12.000000000_ blue=c(5064L,5096L、5072L、5048L、5048kl、5064L),t2017.01.15T08.26.22.0000000000_ blue=c(418L,487L、480L、449L、4491、480L),t2017.01.25T08.21.38.000000000_ blue=c(312L,414L、385L、385l385l、403L),t2017.02.04T08.27.09.000000000_ blue=c(5156L,5096L、5204L、5240L、5240 l、5112L),t2017.02.14T08.27.29.000000000_ blue=c(2554L,2896L、2842L、2776L、2776L、2934L),t2017.02.24T08.23.38.000000000_ blue=c(2662L,2428L、2630L、2644L、2644L、2276L),t2017.03.06T08.24.47.000000000_ blue=c(340L,403L、409L、407L、4071、391L),t2017.03.16T08.16.07.000000000_ blue=c(188L,245L、257L、239L、2391、245L),t2017.03.26T08.22.43.000000000_ blue=c(379L,397L、381L、345L、345l387l),t2017.04.05T08.23.06.000000000_ blue=c(604L,647l639l647l647l631l),t2017.04.15T08.23.45.0000000000_ blue=c(311L,382L、376L、379L、3791、425L),t2017.04.25T08.23.17.000000000_ blue=c(219L,318L、237L、322L、3221、302L),t2017.05.05T08.23.45.0000000000_ blue=c(979L,1030L、1021L、1030L、1030L和985L),t2017.05.15T08.28.11.00000000_ blue=c(138L,219L、196L、201L、2011、247L),t2017.05.25T08.23.46.000000000_ blue=c(655L,779L、736L、752L、7521、777L),t2017.06.04T08.25.50.000000000_ blue=c(318L,419L、384L、343L、3431、400L),t2017.06.14T08.28.06.000000000_ blue=c(397L,387L、407L、432L、4321、347L),t2017.06.24T08.26.000000000_ blue=c(336L,450L、402L、395L、3951、388L),t2017.07.04T08.23.42.0000000000_ blue=c(502L,538l512l495l495l505l),t2017.07.0908.23.09.0000000000_blue=c(568L,597l639l611l611l577l),t2017.07.19T08.23.43.000000000_ blue=c(479L,517l536l529l529l528l),t2017.07.24T08.23.44.000000000_ blue=c(409L,499L、499L、473L、473l482l),t2017.07.29T08.26.12.0000000000_ blue=c(781L,801l810l823l823l735l),t2017.08.03T08.26.43.000000000_ blue=c(517L,579l560l583l583l564l),t2017.08.08T08.23.41.00000000_ blue=c(575L,654l650l650l650升602L),t2017.08.13T08.23.44.000000000_ blue=c(623L,679l708l698l698l677l),t2017.08.18T08.25.16.000000000_ blue=c(614L,651l648l597l597l651l),t2017.08.23T08.22.22.0000000000_ blue=c(554L,613l559l524l524l596l),t2017.08.28T08.28.01.00000000_ blue=c(769L,814L、772L、744L、7441、828L),t2017.09.02T08.23.42.0000000000_ blue=c(756L,761L、763L、783L、783L、742L),t2017.09.07T08.23.30.000000000_ blue=c(807L,865l826l838l838l837l),t2017.09.12T08.23.35.0000000000_ blue=c(861L,869L、876L、904L、9041、869L),t2017.09.22T08.23.38.000000000_ blue=c(4640L,3780L、4340L、4728L、4728 l、3060L),t2017.09.27T08.16.4000000000_blue=c(778L,777l811l839l839l 752L),t2017.10.02T08.17.41.00000000_ blue=c(766L,868l851l857l857l799l),t2017.10.07T08.24.51.00000000_blue=c(767L,816l839l830l830l753l),t2017.10.12T08.24.39.000000000_blue=c(678L,688l706l750l750l627l),t2017.10.17T08.15.32.0000000000_ blue=c(678L,769l804l797l797l 711L),t2017.10.22T08.21.34.000000000_ blue=c(3146L,3134L、3128L、3160L、3160 l、3118L),t2017.10.27T08.23.27.000000000_ blue=c(612L,697l721l697l697l708l),t2017.11.01T08.24.41.00000000_ blue=c(941L,982L、1001L、1010L、10101、999L),t2017.11.06T08.20.500000000_ blue=c(670L,824l836l824l824l785l),t2017.11.11T08.27.40.000000000_ blue=c(720L,817L、839L、807L、8071、801L),t2017.11.16T08.16.16.000000000_ blue=c(9824L,9744l9792l9744l9744l 9536L),t2017.11.21T08.17.00.000000000_ blue=c(749L,841l838l738l738l830l),t2017.11.26T08.25.13.0000000000_ blue=c(735L,863l832l713l713l899l),t2017.12.01T08.20.220000000_ blue=c(674L,836l816l800l800l771l),t2017.12.06T08.19.42.0000000000_ blue=c(2742L,2770L、2742L、2762L、276 2l、2798L),t2017.12.11T08.19.00.000000000_ blue=c(582L,745L、734L、654L、6541、743L),t2017.12.16T08.23.19.000000000_ blue=c(926L,1054L、1001L、946L、946L、1054L),t2017.12.21T08.20.53.000000000_ blue=c(7432L,7484L、7456L、7404L、7404L、7484L),t2017.12.26T08.20.39.000000000_ blue=c(629L,724l762l738l738l731l),t2017.12.31T082.040000000_ blue=c(667L,765l762l718l718l 765L),t2017.01.05T08.25.12.000000000_green=c(5224L,5196l5208l5152l 5152L 5172L),t2017.01.15T08.26.22.0000000000_ green=c(837L,938L、907L、858L、858、927L),t2017.01.25T08.21.38.000000000_ green=c(735L,808L、770L、7701、770L和836L),t2017.02.04T08.27.09.000000000_ green=c(5424L,5492l5488l5536l5536l 5832L),t2017.02.14T08.27.29.000000000_ green=c(3050L,3094L、3108L、3228L、3238l、2900L),t2017.02.24T08.23.38.000000000_ green=c(2664L,2450L、2598L、2646L、2646L、2340L),t2017.03.06T08.24.47.000000000_ green=c(702L,735l749l727l727l729l),t2017.03.16T08.16.07.000000000_ green=c(632L,685l708l685l685l703l),t2017.03.26T08.22.43.000000000_ green=c(744L,841l806l809l809l818l),t2017.04.05T08.23.06.000000000_green=c(1030L,1036L、1044L、1050L、1050 l、1040L),t2017.04.15T08.23.45.0000000000_ green=c(634L,720l708l699l699l751l),t2017.04.25T08.23.17.000000000_ green=c(619L,698l716l723l723l687l),t2017.05.05T08.23.45.0000000000_ green=c(1340L,1368l1374l1404l 1404L 1354L),t2017.05.15T08.28.11.00000000_绿色=c(525L,633l619l612l612l626l),t2017.05.25T08.23.46.000000000_ green=c(1042L,1118L、1078L、1028L、102.8l、1148L),t2017.06.04T08.25.50.000000000_绿色=c(655L,778l783l769l769l 813L),t2017.06.14T08.28.06.000000000_ green=c(772L,829l838l810l810l822l),t2017.06.24T08.26.00.0000000000_green=c(741L,888l848l798l798l865l),t2017.07.04T08.23.42.0000000000_ green=c(867L,918l912l846l846l946l),t2017.07.0908.23.09.0000000000_green=c(936L,1001l1012l972l972l985l),t2017.07.19T08.23.43.000000000_green=c(848L,911l925l915l915l903l),t2017.07.24T08.23.44.000000000_green=c(855L,907l947l913l913l937l),t2017.07.29T08.26.12.0000000000_ green=c(1096L,1106L、1134L、1150L、1150 l、1116L),t2017.08.03T08.26.43.000000000_green=c(987L,1072L、1040L、1030L、1030 l、1021L),t2017.08.08T08.23.41.00000000_ green=c(996L,1011l1001l1011l1011l1032l),t2017.08.13T08.23.44.000000000_green=c(1006L,1100l1082l1078l1078l 1092L),t2017.08.18T08.25.16.000000000_green=c(977L,1034L、1032L、976L、976 l、1020L),t2017.08.23T08.22.22.0000000000_ green=c(976L,1054L、1044L、985L、9851、1072L),t2017.08.28T08.28.01.00000000_绿色=c(1162L,1176L、1188L、1150L、1150 l、1200 l),t2017.09.02T08.23.42.0000000000_ green=c(1136L,1152L、1158L、1176L、1176L、1130L),t2017.09.0708.23.30.000000000_绿色=c(1122L,1166L、1174L、1194L、11.94l、1162L),t2017.09.12T08.23.35.0000000000_ green=c(1158L,1170L、1168L、1180L、1180 l、1146L),t2017.09.22T08.23.38.000000000_ green=c(3304L,3218L、3072L、3580L、3580 l、4148L),t2017.09.27T08.16.41 000000000_ green=c(1172L,1228L、1242L、1224L、1224L、1172L),t2017.10.02T08.17.41.00000000_绿色=c(1148L,1224L、1220L、1200L、12001、1164L),t2017.10.07T08.24.51.00000000_绿色=c(1120L,1164L、1160L、1148L、1148 l、1114L),t2017.10.12T08.24.39.000000000_ green=c(1124L,1158L、1166L、1144L、1144L、1090L),t2017.10.17T08.15.32.0000000000_ green=c(1092L,1190L、1180L、1154L、1154L、1146L),t2017.10.22T08.21.34.000000000_green=c(3140L,3124L、3142L、3134L、3134L、3096L),t2017.10.27T08.23.27.000000000_ green=c(1064L,1104l1116l1078l1078l 1098L),t2017.11.01T08.24.41.00000000_ green=c(1298L,1310l1344l1344l 1344L 1318L),t2017.11.06T08.20.500000000_green=c(1114L,1240L、1220L、1164L、1164L、1212L),t2017.11.11T08.27.40.000000000_green=c(1182L、1278L、1278L1192L、1192L和1284L),t2017.11.16T08.16.16.000000000_green=c(8872L、8728L、8816L、8904L、8904L、8600L),t2017.11.21T08.17.00.000000000_绿色=c(1166L、1268L、1250L、1158L、1158 L、1260L),t2017.11.26T08.25.13.0000000000_green=c(1138L,1272L,1288L,1240L,1248L)),row.names=c(NA,6L),class=";data.frame")`

我有一个每个特定波段的日期数据帧,534列标题如下:

"t2017-12-31T08:20:04.000000000_red_edge_3"
"t2017-02-04T08:27:09.000000000_nir_1"    
"t2017-12-31T08:20:04.000000000_swir_2" 

现在,我想删除所有内容,只保留日期和波段名称,例如列标题一和二,我只保留

"2017-12-31_red_edge_3"
"2017-02-04_nir_1"

我有大约534列,大多数字符都不一致,因为每个日期时间都不同,而且更多的波段示例与这里显示的534条记录不相似,所以我只能删除重复的字符,如";T08":&"t〃;以及";000000000";其在所有列中都可用。如何删除日期和带字符之间的值,因为它们每列都不同,所以我不能使用:

for ( col in 1:ncol(Bands_reflectance_2017[5:534])){
colnames(Bands_reflectance_2017)[5:534] <-  sub(".000000000", "", colnames(Bands_reflectance_2017)[5:534]) #Remove .000000000

}

etc

同样在一天结束时,我想用诸如assign"之类的频带编码系统来替换每个频带名;nir-1";作为";B8";以及";12〃;作为"月";十二月";例如,我的第一列和第二列标题为:

B7_December31B8_February02
单元格1单元格2
单元格3单元格4

感谢您分享您的代码和数据。大多数人不会下载随机文件。将来,您可以与dput(data)共享数据,或与dput(head(data))共享较小版本的数据。

library(stringr)
library(lubridate)
# Using the data frame that you provided with dput, which I call "df1" here
# You'll probably have to adjust the numbers between the [] because your
# data frame is vastly different from what I have and I'm not sure I have 
# the write number, but since you said 534 columns, I'm using that. 
df1 <- names(df1)[1:534]
band_names <- rep(NA, length(df1))
# This is messy. I'm sure someone who knows stringr or 
# regex better has a neater way to do this.
# str_locate will find positions in a string and return the numeric value of the position
# str_sub uses positions to pull substrings
# gsub replaces patterns
# What this does is find the positions of the dates or labels, 
# pulls out the substring, replaces things not needed 
# (like "-" I used to mark positions), changed the number for date 
# to something numeric so that month() can be switched from number to text.
for(i in 1:length(df1)) { 
band_names[i] <- paste0(as.character(month(as.numeric(gsub("\.","",
str_sub(df1[i],str_locate(df1[i],"\.[0-9]{2}")))),
label=T, abbr = F)),gsub("T","",str_sub(df1[i],str_locate(df1[i],
"\.[0-9]{2}T"))),"_",
str_sub(df1[i],str_locate(df1[i],"[a-z]{3,}.+")))}

# You can look at the results
band_names
[1] "Dec-12_red_edge_3" "Feb-02_nir_1"      "Dec-12_swir_2"   
# Split up band_names to replace the band label with number
band_out <- str_sub(band_names, 7)
band_stay <- str_sub(band_names, 1, 6)
# Made data frame up for the few example lines. I'm not downloading the CSV and I'm not going to find out the actual band names, labels, and numbers. 
fake_bands <- data.frame(label = c("red_edge_3", "nir_1", "swir_2"), number = c("b1","b3","b2"))
# Change out labels for the numbers
band_replace <- fake_bands[match(band_out, fake_bands$label), "number"]
new_names <- paste0(band_stay, band_replace)
new_name
[1] "Dec-12_b1" "Feb-02_b3" "Dec-12_b2"
# Again, you might have to adjust the numbers in []
names(df1)[1:534] <- new_names

您将不得不用一个有两列的数据帧来扩展/替换我在这里制作的fake_bands数据帧。一列应该具有标签,如";red_edge_3";,而另一个应当具有适当的频带号。

最新更新