How to "concatenate" and define a variable at the same time

Viewed 77

I got this two data frames:

variables = tibble(x = "A", y = "P")
description = tibble(x = "Area", y = "Perimeter")

And i want to reach this:

list(A = "Area", P = "Perimeter")

How can i do that?

3 Answers

You can unlist the dataframe and create a named list using setNames :

setNames(as.list(unlist(description)), unlist(variables))

#$A
#[1] "Area"

#$P
#[1] "Perimeter"

You can rename the dataframe column names instead of individual values as suggested by @Darren Tsai which make it shorter and simple.

as.list(setNames(description, variables))
l <- as.list(description)
names(l) <- variables

Output

> l
$A
[1] "Area"

$P
[1] "Perimeter"

We can use unclass

setNames(unclass(description), variables)
#$A
#[1] "Area"

#$P
#[1] "Perimeter"
Related