babesz babesz - 3 months ago 26
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
into one column and the
into another and I read that normally this could be done with the
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
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.


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

A %>% separate(type.month, c('type','month'))


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