Using following code to create labels for a factor variable:
group<- c("1", "2", "3")
labels<- paste("c\"", group[1], "\"=\"Reference\",\"", group[2], "\"=\"Level2\",\"",
group[3], "\"=\"Level3\")", sep = "")
group.labels<- eval(parse(text = labels))
```r
When running the last statement group.labels<- eval(parse(text = labels)), got error message:
> group.labels<- eval(parse(text = labels))
Error in parse(text = labels) : <text>:1:2: unexpected string constant
1: c"1"
^
Does anyone know what is the problem? Thank you.
Is there a reason to avoid doing it the usual way?
set.seed(42)
head((object <- data.frame(membership = sample(1:3,30,replace = TRUE))))
#> membership
#> 1 1
#> 2 1
#> 3 1
#> 4 1
#> 5 2
#> 6 2
object$membership <- factor(object$membership,
labels = c("Reference",
"Level2",
"Level3"))
head(object)
#> membership
#> 1 Reference
#> 2 Reference
#> 3 Reference
#> 4 Reference
#> 5 Level2
#> 6 Level2
Created on 2023-04-09 with reprex v2.0.2
Thank you for your response, I am gonna try this way if works in visualization.
1 Like
system
Closed
4
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.