简体   繁体   中英

Read and reshape dataframe in r

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


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM