R 为循环更改列表中的列名
我试图使用R 为循环更改列表中的列名,r,for-loop,R,For Loop,我试图使用for循环来更改相对于以下名称的列名,但是,我似乎无法正确使用它。因此,我非常感谢您对我的方法的支持: for(i in 1:15){ names(test.enc[[i]])[2] <- c("enc_evergreen_needleleaf", "enc_evergreen_broadleaf", "enc_deciduous_needleleaf", "enc_deciduous_broa
for循环
来更改相对于以下名称的列名,但是,我似乎无法正确使用它。因此,我非常感谢您对我的方法的支持:
for(i in 1:15){
names(test.enc[[i]])[2] <- c("enc_evergreen_needleleaf",
"enc_evergreen_broadleaf",
"enc_deciduous_needleleaf",
"enc_deciduous_broadleaf",
"enc_mixed_forest",
"enc_closed_shrubland",
"enc_open_shrubland",
"enc_woody_savanna",
"enc_savanna",
"enc_grassland",
"enc_wetland",
"enc_cropland",
"enc_urban",
"enc_mosaic",
"enc_barren")
}
您可以使用
Map
:
test.enc <- Map(function(x, y) {names(x)[2] <- y;x}, test.enc, cols)
其中cols
为:
cols <- c("enc_evergreen_needleleaf",
"enc_evergreen_broadleaf",
"enc_deciduous_needleleaf",
"enc_deciduous_broadleaf",
"enc_mixed_forest",
"enc_closed_shrubland",
"enc_open_shrubland",
"enc_woody_savanna",
"enc_savanna",
"enc_grassland",
"enc_wetland",
"enc_cropland",
"enc_urban",
"enc_mosaic",
"enc_barren")
cols
for(i in seq_along(test.enc)){
names(test.enc[[i]])[2] <- cols[i]
}
cols <- c("enc_evergreen_needleleaf",
"enc_evergreen_broadleaf",
"enc_deciduous_needleleaf",
"enc_deciduous_broadleaf",
"enc_mixed_forest",
"enc_closed_shrubland",
"enc_open_shrubland",
"enc_woody_savanna",
"enc_savanna",
"enc_grassland",
"enc_wetland",
"enc_cropland",
"enc_urban",
"enc_mosaic",
"enc_barren")