-2

I looking at foreign powers intervening into civil wars using R studio. My first dataset unit of analysis is conflict year while the second one is conflict month. I would need to have both of them in conflict years so I can merge them. Is there any command that allows you to do the opposite of expanding rows?

1 Answers1

0

It's hard to give you specifics without a sample of your data so we know what the structure is. I'm assuming your month-level dataset stores the month as a character string that includes a year. You should be able to extract the year with separate from the tidyr package:

library(tidyverse)

month <- c("June 2015", "July 2015", "September 2016", "August 2016", "March 2014")
conflict <- c("A", "B", "C", "D", "E")

my.data <- data.frame(month, conflict)

my.data
           month conflict
1      June 2015        A
2      July 2015        B
3 September 2016        C
4    August 2016        D
5     March 2014        E

my.data <- my.data %>%
   separate(month, c("month", "year"), sep = " ")

> my.data
      month  year conflict
1      June  2015        A
2      July  2015        B
3 September  2016        C
4    August  2016        D
5     March  2014        E
mmalloy
  • 54
  • 7
  • Sorry if I'm not clear enough, but I have barely learn the basics about R and I am really struggling even copy and paste code keeping the format. – Sara Canto Aug 23 '17 at 08:56
  • But basically my first dataset include a numerical string variable like this: 0198 (which refers to month 01 or year 98). There is no separation so I first struggle to subtract this variable into two variables. – Sara Canto Aug 23 '17 at 08:59
  • In that case, just run this exact same code but change `sep = 2`. This will split your field between the second and third character, so you will end up with 01 and 98. – mmalloy Aug 23 '17 at 14:05