1

I have the following data that I obtained from a .txt file using the read_lines function from readr

txtread<-read_lines("expenses_copy1.txt")
txtread

[1] "Amount:Category:Date:Description"                                           
 [2] "5.25:supply:20170222:box of staples"                                        
 [3] "79.81:meal:20170222:lunch with ABC Corp. clients Al, Bob, and Cy"           
 [4] "43.00:travel:20170222:cab back to office"                                   
 [5] "383.75:travel:20170223:flight to Boston, to visit ABC Corp."                
 [6] "55.00:travel:20170223:cab to ABC Corp. in Cambridge, MA"                    
 [7] "23.25:meal:20170223:dinner at Logan Airport"                                
 [8] "318.47:supply:20170224:paper, toner, pens, paperclips, tape"                
 [9] "142.12:meal:20170226:host dinner with ABC clients, Al, Bob, Cy, Dave, Ellie"
[10] "303.94:util:20170227:Peoples Gas"                                           
[11] "121.07:util:20170227:Verizon Wireless"                                      
[12] "7.59:supply:20170227:Python book (used)"                                    
[13] "79.99:supply:20170227:spare 20\" monitor"                                   
[14] "49.86:supply:20170228:Stoch Cal for Finance II"                             
[15] "6.53:meal:20170302:Dunkin Donuts, drive to Big Inc. near DC"                
[16] "127.23:meal:20170302:dinner, Tavern64"                                      
[17] "33.07:meal:20170303:dinner, Uncle Julio's"                                  
[18] "86.00:travel:20170304:mileage, drive to/from Big Inc., Reston, VA"          
[19] "22.00:travel:20170304:tolls"                                                
[20] "378.81:travel:20170304:Hyatt Hotel, Reston VA, for Big Inc. meeting" 

I want to read each of these in to vectors that are "Amount", "Category", "Date" and "Description" and create a dataframe out of them so that I have a dataset I can work with

I tried the following

for (i in length(txtread) ) {
  data<-read.table(textConnection(txtread[[i]]))
  print(data)
}

However this does't seem to work. how can I read this data into a dataframe in R

Nathan123
  • 763
  • 5
  • 18
  • 7
    If you just want a data.frame use `data<-read.table("expenses_copy1.txt", sep=":", header=TRUE)` (using base R) or you can use `readr` with `data<-read_delim("expenses_copy1.txt", ":")`. No need to bother with the loop. – MrFlick Jul 17 '20 at 00:03

0 Answers0