-2

The first column of my dataframe is a factor that contains two sets of information: the type of activation works (A1-4) and the month when it was carried out (about 50 observations in YYMM format). A simplified version could look like this:

A = data.frame(type.month=c("A1.1605", "A2.1605", "A1.1604", "A2.1604"), value=sample(1:4))

> A
  type.month value
1    A1.1605     2
2    A2.1605     4
3    A1.1604     1
4    A2.1604     3

I would like to get the types into one column and the months into another and I read that normally this could be done with the reshape2 package when the variables are neatly separated (say e.g. the first half is only A1 and the second half is only A2). However, mine alternate (A1,A2,A1...) and contain two information (type and month). Is reshape2 still a good tool in this case or I should think about something else?

My point is to keep the four type of activation works and months in one dataframe so that I do not have to store them in four different files.

parksw3
  • 649
  • 4
  • 11
babesz
  • 43
  • 1
  • 9
  • 1
    You're going to want stringr. Check this out! http://stackoverflow.com/questions/4350440/split-a-column-of-a-data-frame-to-multiple-columns – Joy Oct 14 '16 at 18:42
  • 1
    Joy, thanks, I'll check it out! @Frank, hope the title is better now. – babesz Oct 14 '16 at 18:46

2 Answers2

2

This separates the string using tidyr function separate:

A = data.frame(type.month=c("A1.1605", "A2.1605", "A1.1604", "A2.1604"), value=sample(1:4))


library(dplyr)
library(tidyr)
A %>% separate(type.month, c('type','month')) %>% arrange(type, desc(month))

gives

type  month      value
A1    1605       4
A1    1604       2
A2    1605       1
A2    1604       3
Wietze314
  • 5,942
  • 2
  • 21
  • 40
0

Drat, I forgot to mention regular expressions. You'll have to escape out the period like this:

library(stringr)
str_split_fixed(A$type.month, "\\.", 2)
Jaap
  • 81,064
  • 34
  • 182
  • 193
Joy
  • 769
  • 6
  • 24