0

x is a data.frame

id  income  age 
1   23246  21
1   12455  45
3   24555  31
2   10045  42

I would like to have 3 data frames based on id number

Maurits Evers
  • 45,165
  • 4
  • 35
  • 59
Y.esk
  • 11
  • 3

2 Answers2

1

split(x,x$id) will produce a list of data frames split on id.

George Savva
  • 2,316
  • 1
  • 5
  • 16
  • I am sorry, it looks trivial, but I want to save the results of corresponding values for each id in a data frame. Like a loop or sth does it to get the result x1 – Y.esk Feb 24 '20 at 21:53
  • 1
    @Y.esk It is advisable to store the splitted `data.frame`s in a `list` (rather than as "loose" objects in your environment). The `split` command generates a `list`, which then can be conveniently processed/manipulated using functions from the `*apply` family. – Maurits Evers Feb 24 '20 at 21:57
0

I guess what you need is list2env, i.e.,

xs <- split(x,x$id)
list2env(setNames(xs,paste0("df",names(xs))),
         envir = .GlobalEnv)

which creates data frames df1, df2 and df3 in your global environment, such that

> df1
  id income age
1  1  23246  21
2  1  12455  45
> df2
  id income age
4  2  10045  42
> df3
  id income age
3  3  24555  31
ThomasIsCoding
  • 80,151
  • 7
  • 17
  • 65