Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 201
Vinicius B. de S. Moreira Avatar asked Dec 11 '25 07:12

Vinicius B. de S. Moreira


1 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))
like image 52
Ronak Shah Avatar answered Dec 12 '25 21:12

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!