0

When I want to label the variables of my dataframe using german umlaute or superscript etc... with this code everything works fine:

library(dplyr)
library(Hmisc)

## with this code everthing works fine
# dataframe
colnamesiris <- c("a", "b", "c", "d")
iris_test <- iris %>% 
  select(2:5) %>% 
  setNames(colnamesiris)

# defining labels
df_test_labels <- c(
  a = "Fake1 [cm²]",
  b  = "Fake2 ä",
  c = "Fake3 Ö",
  d = "üüü³²³")

# assign labels to dataframe (iris_test)
iris_test <- Hmisc::upData(iris_test, labels = df_test_labels)
View(iris_test)

enter image description here

  • In order to clean up workflow I split this code into two files 1. analysis.R with sourcing the second file containing the label definitions 2. df_test_labels.R (vector of label definitions). In this case the labels do not appear appropriate.
  1. analysis.R
library(dplyr)
library(Hmisc)

## with this code everthing works fine
# dataframe
colnamesiris <- c("a", "b", "c", "d")
iris_test <- iris %>% 
  select(2:5) %>% 
  setNames(colnamesiris)

# assign labels to dataframe (iris_test)

source("./df_test_labels.R") # vector for labels

iris_test <- Hmisc::upData(iris_test, labels = df_test_labels)
View(iris_test)
    1. df_test_labels.R
# defining labels
df_test_labels <- c(
  a = "Fake1 [cm²]",
  b  = "Fake2 ä",
  c = "Fake3 Ö",
  d = "üüü³²³")

enter image description here

  • I found out that this behavior occurs only when iris_test <- Hmisc::upData(iris_test, labels = df_test_labels) is not in the same script with the definition of the variable labels. Can someone explain why this is occurring? Thank you in advance.
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    On my Mac your code runs fine but on my windows machine I have to use `source(..., encoding="UTF-8")` when sourcing scripts so that German umlaute work properly. – stefan Jan 30 '21 at 19:02
  • Solved! Thank you stefan. – TarJae Jan 31 '21 at 05:27

0 Answers0