3

I need that each column of my dataset become an object named with that column's name and containing its values as the object value.

I know how to do the process manually (see "Process question" heading below), but I need to automatize and generalize the process with as few rows as possible.

Example data
library(tibble)
df <- tibble(a = 1, b = 2, c = 3, d = 4)
Input
> df
# A tibble: 1 x 4
      a     b     c     d
  <dbl> <dbl> <dbl> <dbl>
1     1     2     3     4
    

Process Question

How to automatize this part?

a <- df$a
b <- df$b
c <- df$c
d <- df$d

Output

> a;b;c;d
[1] 1
[1] 2
[1] 3
[1] 4
2
  • 1
    Why do you need to do this? Folks will often caution against putting lots of objects into the global environment if those objects make sense being held together in some larger structure—that's often a list, of which data frames are a type Commented Feb 13, 2022 at 18:07
  • Now that you mention it, I see I can accomplish my goals without the need to have separated objects. I can directly use them inside the list or dataframe as you mentioned. Thanks for opening my eyes for me that! and now, it seems obvious. I was just fixed in the mindset that I needed the objects outside the dataframe. Commented Feb 13, 2022 at 21:38

1 Answer 1

4

tibble/data.frame are list. So, we can use list2env (or use attach)

list2env(df, .GlobalEnv)

-checking

> a
[1] 1
> b
[1] 2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.