0

I am trying to extract labels attribute from a number of columns from a stata .dta file into a dataframe.

However the attributes(data$datattr) function returns NULL value for some reason. Could you help out with this ?

    data <- read_dta('C:\\Users\\...) 
    colnames1 <- data.frame(colnames(data)) 
    colnames1$var_descr = NA

    for (i in 1:nrow(colnames1)){
      dataattr <- colnames1[i,1]
      list_of_attributes <- attributes(data$datattr)
      colnames1[i,2] <- list_of_attributes[[1]]
     }

I am expecting this loop to populate the empty column of colnames1 dataframe. A better solution for getting the variables and labels in one place would also interest me, I am new to coding.

Thanks in advance!

robipham
  • 1
  • 2
  • 1
    Try `attributes(data[[datattr]])`. `...$datattr` will return the column with name `datattr`. And if there is no column with such a name will return `NULL` – stefan Dec 06 '22 at 14:09

0 Answers0