Reputation: 101
I have the following csv file
Column1
" [1] ""offer1"" ""offer2"" ""offer3"" "
" [4] ""offer2"" ""offer2"" ""offer1"" "
" [7] ""offer3"" ""offer1"" ""offer2"""
"[10] ""offer1"" ""offer2"" ""offer3"" "
I tried to import it in r using read.csv and readLines but the outcome is very messy I would like to achieve the following dataframe
Column1
1 offer1
2 offer2
3 offer3
4 offer2
5 offer2
6 offer1
7 offer3
8 offer1
9 offer2
Upvotes: 0
Views: 21
Reputation: 11594
Does this work:
> library(stringr)
> library(tidyr)
> library(dplyr)
> df %>% mutate(column1 = str_extract_all(column1, 'offer\\d+')) %>% separate_rows() %>% unnest(column1)
# A tibble: 12 x 1
column1
<chr>
1 offer1
2 offer2
3 offer3
4 offer2
5 offer2
6 offer1
7 offer3
8 offer1
9 offer2
10 offer1
11 offer2
12 offer3
>
Data used:
structure(list(column1 = " [1] \"\"offer1\"\" \"\"offer2\"\" \"\"offer3\"\" \"\n \" [4] \"\"offer2\"\" \"\"offer2\"\" \"\"offer1\"\" \"\n \" [7] \"\"offer3\"\" \"\"offer1\"\" \"\"offer2\"\"\"\n \"[10] \"\"offer1\"\" \"\"offer2\"\" \"\"offer3\"\" "), class = "data.frame", row.names = c(NA,
-1L))
>
As Ricardo asked if you are looking for something else, then please add it in your question.
Upvotes: 1