Question

How to convert data frame in R to a "character" data type?

The labeller function which I'm using for ggplot requires data of the type:

tempdat <- c(
   "1" = "one",
   "2" = "two"
)

If I use typeof(tempdat) it gives an output saying that it is of type character.

I have a data frame that looks like:

> tempdf
    parent name
1       1  one
2       2  two

I want to convert my data frame tempdf to the same data type as tempdat. How do I do this?

 2  51  2
1 Jan 1970

Solution

 3

You can try

with(tempdf, setNames(name, parent))
2024-07-23
ThomasIsCoding

Solution

 2

You can also use do.call with setNames:

do.call(setNames, unname(tempdf[2:1]))
#     1     2 
# "one" "two"

or deframe from {tibble}:

tibble::deframe(tempdf)
#     1     2 
# "one" "two"
2024-07-23
Darren Tsai