-1

Hello i'm trying to create a data frame inside a loop, this data frame should have only matching values, i'm trying to implement a logic like that:

names<- unique(list(data$costumers))
for (i in 1:length(names)) {
    city <- data$city where data$costumers == names[i]
    costumer <- data$costumers where data$costumers == names[i] 
    df <- data.frame(costumer,city)
} 

Basically i'm trying to make a data frame for each unique name in the list, i don't know how to compare in a data frame, i've tried the if statement but i couldn't get it to work.

a example dataframe input would be something like that:

costumer     city
Joseph       WS
Edward       WS
Joseph       NY

so the output dataframe would be like this:

costumer    city
Joseph      WS
Joseph      NY

and the second dataframe output would be like that:

costumer    city
Edward      WS

In conclusion i'm trying to get a single data frame for every unique name in the list, and that data frame should have all the rows that include that name.

Wel
  • 201
  • 2
  • 13
  • 1
    Can you please share your data (with `dput`), and show what your expected output is? – Conor Neilson Jun 04 '18 at 19:13
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 04 '18 at 19:18

1 Answers1

0

you can use split:

split(data, data$customer)
$Edward
  customer city
2   Edward   WS

$Joseph
  customer city
1   Joseph   WS
3   Joseph   NY

you can either refer to the dataframes from this list or even

 list2env(split(data, data$customer))

and now just call the dataframes by the customer's name

Onyambu
  • 67,392
  • 3
  • 24
  • 53