I have a dataset composed of columns including: Indicator, Value, Province, goal number, and ranking. Based on this dataset, I would like to make provincial datasets. In other words, I would like to make subsets of the dataset titled by the name of province. How can I do it this with for loop? Or is there any other way? Thanks!
Asked
Active
Viewed 85 times
-2
-
Have a look at the `split` function. – James Sep 13 '17 at 10:48
-
3Try `split(df,factor = df$Province)` where `df` is your `data.frame`. People will be able to help you in a much better way if you could share a sample of your data in the question description. You can share output of `dput(head(df))` in the question description. – tushaR Sep 13 '17 at 10:48
2 Answers
1
Cosidering your data frame name as 'df' and the column on which you want to split into small data frames as 'Province' use below code
library(plyr)
list_df = dlply(df, "Province", identity)
list2env(list_df,envir=.GlobalEnv)

Sai Prabhanjan Reddy
- 536
- 3
- 16
0
Welcome to stack overflow.
Assume you have a data.frame
like this:
ex <- data.frame("Indicator" = c(1,1,2,1), "Value" = c(2,3,4,1),
"Province" = c("a","a","b","b"), "goal number" = c(1,1,1,1),
"ranking" = c(1,1,5,5))
ex
Indicator Value Province goal.number ranking
1 1 2 a 1 1
2 1 3 a 1 1
3 2 4 b 1 5
4 1 1 b 1 5
then you can use subset
to make subsets of your data.frame
:
subset(ex, ex$Province == "a")
Indicator Value Province goal.number ranking
1 1 2 a 1 1
2 1 3 a 1 1
For future questions: Please provide an reproducible example.

smoff
- 592
- 5
- 21