I am trying to create on a dummy dataset using the following code to create the variables:
example_variable <- labelled(sample(c(1, 5, 8), N, replace = T),
labels = c("apple" = 1,
"banana" = 5,
"lychee" = 8))
All good, but I am working with an example data set that is empty, so I have copy the categorical variables from this, for example:
<labelled<double>[0]>: Leeftijd [4 klassen]
Labels:
value label
1 15 tot 25 jaar
2 25 tot 55 jaar
3 55 tot 65 jaar
4 65 tot 75 jaar
9 onbekend
However, some of them have many, many categories so typing it would be very time consuming. Also the numbers are not always in a logical sequence. What I am trying to do is the following:
copy the labels and put them as text;
then extract the numbers using:
numbers <- unlist(str_extract_all(text, "(?<!\\d)\\d+"))
numbers <- as.numeric(numbers)
extract the text using (the -1 is because i get an empty character string in the end):
d <- head(unlist(str_split(text, "(?<!\\d)\\d+")), -1)
d <- gsub("[\r\n]", "", d)
d <- str_trim(d, side = "both")
My idea was to paste this together with an equal sign in the middle and a comma at the end, but i am fighting with the quotation marks. Regular paste() or paste0() does not work and neither does unquote():
d <- c("apple", "banana", "lychee")
numbers <- c(1, 5, 8)
paste(d, "=", numbers)
[1] "apple = 1" "banana = 5" "lychee = 8"
paste0("\"", d, "\" = ", numbers, "\"", ",")
[1] "\"apple\" = 1\"," "\"banana\" = 5\"," "\"lychee\" = 8\","
paste0("\"", d, "\" = ", numbers, ",")
[1] "\"apple\" = 1," "\"banana\" = 5," "\"lychee\" = 8,"
paste(d, eply::unquote("="), eply::unquote(numbers))
[1] "apple = 1" "banana = 5" "lychee = 8"
Can anyone help me to get the labels in the proper format?
These post did not help (for this specific problem):
R paste two string with an equal sign between it : "stringA" = "stringB"
Unquote strings in list R
I read a lot of other posts, but I can't seem to find what I need.