babesz babesz - 1 month ago 17
R Question

How to separate a column

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.

Answer

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'))

gives

type  month      value
A1    1605       4
A2    1605       3
A1    1604       1
A2    1604       2
Comments