简体   繁体   中英

How could I split a data.frame?

I have 50 synoptic stations precipitation data from 1986 to 2015.

I need to sort the related information for the period of years from 2007 to 2015 for each station separately. I mean there are three variables:

  1. the station's name
  2. the specific year
  3. the amount of precipitation

I need the result for each station separately. Does anyone know how to use "split" for this purpose? May you please write codes from the beginning "read.table"?

If your task is simply to split the dataframe by year you can use split :

split(df, f = df$year)

Illustrative data:

(set.seed(123)
df <- data.frame(
  station = sample(LETTERS[1:3],10, replace = T),
  year = paste0("201", sample(1:9, 10, replace = T)),
  precipitation = sample(333:444, 10, replace = T)
)

Result:

$`2011`
  station year precipitation
5       C 2011           406
8       C 2011           399

$`2013`
  station year precipitation
7       B 2013           393
9       B 2013           365

$`2015`
  station year precipitation
2       C 2015           410

$`2016`
  station year precipitation
4       C 2016           444

$`2017`
  station year precipitation
3       B 2017           404

$`2019`
   station year precipitation
1        A 2019           432
6        A 2019           412
10       B 2019           349

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM