Moksh
Moksh

Reputation: 199

how to store a subset of a data frame in another data frame based on some conditions

I have a csv file which has few observations based on 2 cities. i have added a city with as 1 and 2 to differentiate. i want to create 2 separate data frames on different cities. I have used 2 approaches.

  1. subset function y <- subset(x, x$code == 1)
  2. indexing approach y <- x[x$code ==1, ]

Both the statements were executed successfully but when i enter y data frame , i get only the column names as the output with 0 rows.

Can anyone help me with an alternate method or point out the error in approach i am using?

  Code       city  pickup_datetime pickup_date Hour          Hour range
1    1 Chandigarh 24-03-2016 00:00  24-03-2016    0 00:00:00 - 01:00:00
2    1 Chandigarh 24-03-2016 01:45  24-03-2016    1 01:00:00 - 02:00:00
3    1 Chandigarh 24-03-2016 02:00  24-03-2016    2 02:00:00 - 03:00:00
4    1 Chandigarh 24-03-2016 03:15  24-03-2016    3 03:00:00 - 04:00:00
5    2   Ludhiana 24-03-2016 00:45  24-03-2016    0 00:00:00 - 01:00:00
6    2   Ludhiana 24-03-2016 01:46  24-03-2016    1 01:00:00 - 02:00:00
7    2   Ludhiana 24-03-2016 02:30  24-03-2016    2 02:00:00 - 03:00:00

data.frame': 48 obs. of 20 variables: $ Code : int 1 1 1 1 1 1 1 1 1 1 ... $ city : Factor w/ 2 levels "Chandigarh","Ludhiana": 1 1 1 1 1 1 1 1 1 1 ... $ pickup_datetime: Factor w/ 43 levels "24-03-2016 00:00",..: 1 3 5 7 9 10 11 14 15 18 ... $ pickup_date : Factor w/ 1 level "24-03-2016": 1 1 1 1 1 1 1 1 1 1 ... $ Hour : int 0 1 2 3 4 5 6 7 8 9 ... $ Hour.range : Factor w/ 24 levels "00:00:00 - 01:00:00",..: 1 2 3 4 5 6 7 8 9 10 ...

Upvotes: 0

Views: 1408

Answers (3)

Venkat
Venkat

Reputation: 387

You can try this:

y<-data.frame(subset(x[x$Code==1,]))

Where, x--parent data frame, y--data frame with only code=1

Upvotes: 0

akrun
akrun

Reputation: 886938

I would use

y <- subset(x, Code==1)

as the column name is "Code" and not "code". Just note that there is no need for x$Code inside the subset.


If we need to create two datasets for each "Code", we can split the data.frame to a list of data.frames.

lst <- split(x, x$Code)

If this is needed as individual data.frames in the global environment,

list2env(setNames(lst, paste0("y", names(lst))), 
                       envir= .GlobalEnv)

Upvotes: 0

wotter
wotter

Reputation: 528

Try this:

y <- subset(x, x$Code == 1)

y <- x[x$Code ==1, ]

Upvotes: 0

Related Questions