迭代:一次标记多列
Iteration: labelling multiple columns at once
我需要一次标记多个变量。我想使用的基本方法是
iris<–iris
attr(iris$Sepal.Length, "label") <- "Sepal Length"
我想在我的所有变量中重复这个过程。有没有紧凑的方法来做到这一点?
我已经创建了一个变量名称和标签列表
# First create my labels list.
lablist<-str_to_title(str_replace_all(names(iris), pattern = "\.", replacement = " "))
# Second my variable names list
namelist<- names(iris)
但是当我尝试重复这个时,我的方法失败了:我做错了什么?
# I attempt iteration
map2(namelist,lablist,~attr(iris[,.x], "label") <- .y)
理想情况下,我想使用 map
进行迭代,但如果它们更紧凑和有效,我愿意接受其他解决方案
非常感谢您的帮助
已编辑:重写以解决标签而不是名称的变化
不像循环那样优雅,但确实有效:
iris_labelled <- iris
lablist<-str_to_title(str_replace_all(names(iris_labelled), pattern = "\.", replacement = " "))
namelist<- names(iris)
names(lablist) <- namelist
for (name in namelist) {
attr(iris_labelled[,name], 'label') <- lablist[[name]]
}
attr(iris_labelled$Sepal.Length, 'label')
应该可以(据我所知),但不能:
map2(namelist, lablist, function(x,y) {attr(iris_labelled[,x], "label") <- y})
attr(iris_labelled$Sepal.Length, 'label')
试试这个
replace_labels <- function(df,new_labels){
column_names <- colnames(df)
purrr::map2(column_names, new_labels, ~ (df[[.x]] <-`attr<-`(df[[.x]], "label", .y)))
}
iris_labelled <- iris
lablist<-str_to_title(str_replace_all(names(iris), pattern = "\.", replacement = " "))
iris_labelled[] <- replace_labels(iris_labelled,lablist)
您可以在 Map
中使用 `attr<-`
。
iris <- Map(`attr<-`, iris, "label", c("Sepal Length", "Sepal Width",
"Petal Length", "Petal Width", "Species"))
attributes(iris$Sepal.Length)
# $label
# [1] "Sepal Length"
attributes(iris$Petal.Width)
# $label
# [1] "Petal Width"
我需要一次标记多个变量。我想使用的基本方法是
iris<–iris
attr(iris$Sepal.Length, "label") <- "Sepal Length"
我想在我的所有变量中重复这个过程。有没有紧凑的方法来做到这一点?
我已经创建了一个变量名称和标签列表
# First create my labels list.
lablist<-str_to_title(str_replace_all(names(iris), pattern = "\.", replacement = " "))
# Second my variable names list
namelist<- names(iris)
但是当我尝试重复这个时,我的方法失败了:我做错了什么?
# I attempt iteration
map2(namelist,lablist,~attr(iris[,.x], "label") <- .y)
理想情况下,我想使用 map
进行迭代,但如果它们更紧凑和有效,我愿意接受其他解决方案
非常感谢您的帮助
已编辑:重写以解决标签而不是名称的变化
不像循环那样优雅,但确实有效:
iris_labelled <- iris
lablist<-str_to_title(str_replace_all(names(iris_labelled), pattern = "\.", replacement = " "))
namelist<- names(iris)
names(lablist) <- namelist
for (name in namelist) {
attr(iris_labelled[,name], 'label') <- lablist[[name]]
}
attr(iris_labelled$Sepal.Length, 'label')
应该可以(据我所知),但不能:
map2(namelist, lablist, function(x,y) {attr(iris_labelled[,x], "label") <- y})
attr(iris_labelled$Sepal.Length, 'label')
试试这个
replace_labels <- function(df,new_labels){
column_names <- colnames(df)
purrr::map2(column_names, new_labels, ~ (df[[.x]] <-`attr<-`(df[[.x]], "label", .y)))
}
iris_labelled <- iris
lablist<-str_to_title(str_replace_all(names(iris), pattern = "\.", replacement = " "))
iris_labelled[] <- replace_labels(iris_labelled,lablist)
您可以在 Map
中使用 `attr<-`
。
iris <- Map(`attr<-`, iris, "label", c("Sepal Length", "Sepal Width",
"Petal Length", "Petal Width", "Species"))
attributes(iris$Sepal.Length)
# $label
# [1] "Sepal Length"
attributes(iris$Petal.Width)
# $label
# [1] "Petal Width"