How to remove a deeper attribute from a variable

Hi ,

this works:

attr(temp$d1, "labels") <- attr(temp$d1, "labels")[-1]

attr(temp$d2, "labels") <- attr(temp$d2, "labels")[-1]

attr(temp$d3, "labels") <-  attr(temp$d3, "labels")[-1]

attr(temp$d4, "labels") <-  attr(temp$d4, "labels")[-1]

attr(temp$d5, "labels") <-  attr(temp$d5, "labels")[-1]

attr(temp$d6, "labels") <-  attr(temp$d6, "labels")[-1]

attr(temp$d7, "labels") <-  attr(temp$d7, "labels")[-1]

attr(temp$d8, "labels") <-  attr(temp$d8, "labels")[-1]

attr(temp$d9, "labels") <-  attr(temp$d9, "labels")[-1]

so in order to automate it I have written:

for(i in 2:dim(temp)[2]){
  
attr(temp[,i],"labels") <- attr(temp[,i],"labels")[-1]
  
}

but it doesn't work.

That code below doesn't work as well:

for(i in 2:10){
  
attr(temp[,i],"labels") <- attr(temp[,i],"labels")[-1]
  
}

temp is the name of my dataframe, I want to apply this to columns 2:10.
Please help, thank you.

Hi, can you provide a reproducible example of the dataset temp?

Good morning, and here you are:

temp  <-  structure(list(d1 = structure(c(NA, NA, 1, 2, 1, NA, 4, 1, 2, 
2), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d2 = structure(c(NA, NA, 1, 2, 1, NA, 4, 1, 2, 2), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d3 = structure(c(NA, NA, NA, 2, 1, NA, 3, NA, 2, 2), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d4 = structure(c(NA, NA, NA, 2, NA, NA, 4, 4, 1, 1), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d5 = structure(c(3, 4, 4, 2, 1, 1, 1, 4, 4, 3), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d6 = structure(c(2, 1, NA, 2, 2, NA, 1, 1, NA, 3), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d7 = structure(c(4, 2, NA, 2, 2, NA, 3, 4, 2, 4), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d8 = structure(c(2, 2, NA, 2, 2, NA, 3, 4, 3, 4), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d9 = structure(c(1, NA, NA, 2, 1, NA, NA, 1, 1, 1), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1, 
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
))), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

I would like for example to remove (or exclude) the label "'not applicable' = 0".
I thought that for loop should work, but it did not.
Also I want to ask if is it a faster way to write this:

attr(temp$d1, "labels") <- attr(temp$d1, "labels")[-1]

attr(temp$d2, "labels") <- attr(temp$d2, "labels")[-1]

attr(temp$d3, "labels") <-  attr(temp$d3, "labels")[-1]

attr(temp$d4, "labels") <-  attr(temp$d4, "labels")[-1]

attr(temp$d5, "labels") <-  attr(temp$d5, "labels")[-1]

attr(temp$d6, "labels") <-  attr(temp$d6, "labels")[-1]

attr(temp$d7, "labels") <-  attr(temp$d7, "labels")[-1]

attr(temp$d8, "labels") <-  attr(temp$d8, "labels")[-1]

attr(temp$d9, "labels") <-  attr(temp$d9, "labels")[-1]

When I write it one by one it works, otherwise it does not.

Here you're not actually removing them:

attr(temp$d1, "labels") <- attr(temp$d1, "labels")[-1]

because when you run attr(temp$d1, "labels") again, the 'not applicable' label is still there.

Perhaps try this?

https://www.rdocumentation.org/packages/labelled/versions/2.8.0/topics/remove_labels

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.