Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String separated by \n to dataframe

I have the following string:

  "Title\nToday 1,239 €\nYesterday 1,2 €\n17/04/2018 1,2 €\n14/04/2018 1,2 €\n13/04/2018 1,2 €\n12/04/2018 1,2 €\n11/04/2018 1,2 €\n09/04/2018 1,2 €\n08/04/2018 1,2 €\n07/04/2018 1,2 €"

But I don´t know if I can get a dataframe from it. I want to get a dataframe with two columns (Date and Price) with my string as follows (don't really need the Title name):

Date       Price
Today      1,239 €
Yesteday   1,2 €
17/04/2018 1,2 €
14/04/2018 1,2 €
13/04/2018 1,2 €
12/04/2018 1,2 €
11/04/2018 1,2 €
09/04/2018 1,2 €
08/04/2018 1,2 €
07/04/2018 1,2 €

This is almost the same that I can get with cat function. But I think I can convert it to dataframe. Any ideas?

like image 730
User 6683331 Avatar asked Nov 29 '22 21:11

User 6683331


1 Answers

Here a solution with read.table:

> read.table(text=str, sep=' ', skip=1, col.names=c('Date', 'Price', 'Currency'))
         Date Price Currency
1       Today 1,239        €
2   Yesterday   1,2        €
3  17/04/2018   1,2        €
4  14/04/2018   1,2        €
5  13/04/2018   1,2        €
6  12/04/2018   1,2        €
7  11/04/2018   1,2        €
8  09/04/2018   1,2        €
9  08/04/2018   1,2        €
10 07/04/2018   1,2        €

with str being your data. Note the argument skip is removing the 'Title'.

like image 198
user1981275 Avatar answered Dec 04 '22 02:12

user1981275