0

I have data like this

col1
33
924
33
12
924

and a dataframe like this

col1   col2
12     "London"
33     "Paris"
924    "Singapore"

How do I label the first dataframe based on the columns in the second dataframe using r's labelled?

I know that using val_labels() I can apply a value label to values in a column using:

val_labels(df$col1) <- c(London = 12, Paris = 33, Singapore = 924)

But I have 1000 different values and need an approach that allows me to use a dataframe to do it.

Victor Nielsen
  • 443
  • 2
  • 14

2 Answers2

0

You can try this, though there are likely more elegant solutions:

df <- data.frame(col1 = c(33, 924, 33, 12, 924))
dic <- data.frame(col1 = c(12, 33, 924),
                  col2 = c("London","Paris","Singapore"))

library(labelled)
ct <- 1
for(i in dic$col1){
  val_label(df, i) <- as.character(dic[ct,2])
  ct <- ct+1
}
str(df)
# > str(df)
# 'data.frame': 5 obs. of  1 variable:
#   $ col1: dbl+lbl [1:5]  33, 924,  33,  12, 924
# ..@ labels: Named num  12 33 924
# .. ..- attr(*, "names")= chr [1:3] "London" "Paris" "Singapore"
# > 
jpsmith
  • 11,023
  • 5
  • 15
  • 36
0

With val_labels:

library(labelled)

col1=c(33,924,33,12,924)

ref <- read.table(text='
col1   col2
12     "London"
33     "Paris"
924    "Singapore"',header=T)

val_labels(col1)<-setNames(ref$col1,ref$col2)

col1
#> <labelled<double>[5]>
#> [1]  33 924  33  12 924
#> 
#> Labels:
#>  value     label
#>     12    London
#>     33     Paris
#>    924 Singapore
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • I get this error: Error in `stop_vctrs()`: ! Can't convert `labels` to match type of `x` . – Victor Nielsen Mar 10 '22 at 16:59
  • do you get the error with the above dataset, or on other data? This is working fine for me on `R 4.1.0` with `reprex` package – Waldi Mar 10 '22 at 18:37
  • what is `class(col1)` for you, `integer` as in my code or `character`? both `col1` should have the same datatype – Waldi Mar 10 '22 at 18:43
  • They are both integer. I couldn't get it to work, but your solution works for the data I provided, so I can't ask for more. – Victor Nielsen Mar 11 '22 at 11:37