How to successively extract one row from a datafram?


I have made a simulation program in R. My output is a dataframe that includes a coloum with names. I want to store new dataframes with each individual row from the output dataframe. The new dataframes should be named equal to the name colum. The names in the output could change for each simulation I do. Thus, I wonder if it is possible to automaticly, where I do not need to specify the names in the r code?

So, here is my output:
df <- data.frame(Name= c("Marta", "Per", "Charleen"), x=c(1, 2, 3), y=c(5, 0, 9) )

Then I want to have three dataframes named Marta, Per and Charleen like this:

Marta <- c("Marta", 1, 5)
Per <- c("Per", 2, 0)
Charleen <- c("Charleen", 3, 9)

I am very thankful for all help!

Regards Marit

Here is a method for doing what you want. However, it is an unusual way to handle data in R. If you explain more about what you want to accomplish, someone may have a better solution.

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union

df <- data.frame(Name= c("Marta", "Per", "Charleen"), x=c(1, 2, 3), y=c(5, 0, 9) )
Names <- unique(df$Name)
for (Nm in Names) {
  tmp = df %>% filter(Name == Nm)
  assign(Nm, tmp)
#>    Name x y
#> 1 Marta 1 5

Created on 2021-01-19 by the reprex package (v0.3.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.