babesz - 12 days ago 4

R Question

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`

`months`

`reshape2`

`reshape2`

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

Source (Stackoverflow)

Comments