0

Suppose I have this list containing year and month:

l <- c(200101, 200102 ,200103, 200104)

I want to separate them so that I can have then into 2 separate lists. Here is what I did with no luck:

as.Date(l,"%Y%m",format = "%Y")
Geo-sp
  • 1,704
  • 3
  • 19
  • 42

4 Answers4

4

You could do:

rbind(floor(l/100), l%%100)

#     [,1] [,2] [,3] [,4]
#[1,] 2001 2001 2001 2001
#[2,]    1    2    3    4
989
  • 12,579
  • 5
  • 31
  • 53
3

This would work :

year_list<-as.numeric(substr(as.numeric(l),1,4))
month_list<-as.numeric(substr(as.numeric(l),5,6))

Example 1:

> l <- c(200101, 200102 ,200103, 200104)
> l
[1] 200101 200102 200103 200104

> year_list<-as.numeric(substr(as.numeric(l),1,4))
> year_list
[1] 2001 2001 2001 2001

> month_list<-as.numeric(substr(as.numeric(l),5,6))
> month_list
[1] 1 2 3 4

Example 2:

> l <- c(200701, 200802 ,200903, 201604)
> year_list<-as.numeric(substr(as.numeric(l),1,4))
> year_list
[1] 2007 2008 2009 2016
> month_list<-as.numeric(substr(as.numeric(l),5,6))
> month_list
[1] 1 2 3 4
Ram K
  • 1,746
  • 2
  • 14
  • 23
3

tidyr::separate can take an integer position to split on, but it only takes data.frames. That's not all bad here, though, as it's a nice output format anyway:

library(tidyr)

data.frame(l = c(200101, 200102 ,200103, 200104)) %>% 
    separate(l, into = c('year', 'month'), sep = 4)
##   year month
## 1 2001    01
## 2 2001    02
## 3 2001    03
## 4 2001    04
alistaire
  • 42,459
  • 4
  • 77
  • 117
1

We can use strsplit from base R

 do.call(rbind, lapply(strsplit(as.character(l), 
                  '(?<=.{4})', perl=TRUE), as.numeric))

Or another base R option by creating a delimiter between the characters and use read.csv

read.csv(text=sub("(.{4})(.{2})", "\\1,\\2", l), header=FALSE, 
           col.names= c("Year", "Month"))
#   Year Month
#1 2001     1
#2 2001     2
#3 2001     3
#4 2001     4
akrun
  • 874,273
  • 37
  • 540
  • 662