0

I am using readLines("file.txt") to read in R. I'm getting a character vector:

[1]"classLabel|UserName|2|1.857|Subejct|User Questio(text)n?|Answer(text).|text"

but I wanted to further process it as so i can have it as a dataframe

col1       col2      col3  col4  col5    col6     col7    col8
classLabel  usrName    2  1.857  Subject User Q   Answer   text

The file format is | delimited and there are 8 attributes.

classLabel|UserName|2|1.857|Subejct|User Questio(text)n?|Answer(text).|text

Also if you can share some tutorials or resources for loading Data in R would be really helpful

eykanal
  • 26,437
  • 19
  • 82
  • 113
pandhale
  • 199
  • 1
  • 3
  • 8
  • You might have better luck with questions like this on a sister site http://stats.stackexchange.com/ – Joshua Enfield Feb 29 '12 at 03:47
  • 1
    @JoshuaEnfield This question seems more appropriate for SO than the stats stack exchange site. There is nothing about this question that has to do with stats other than they are using R which is primarily for statistics. – Dason Feb 29 '12 at 03:59
  • @JoshuaEnfield - for what it's worth, there's actually a very strong R support presence here on SO. I've asked a few questions and have always gotten quick, comprehensive replies. Its something that probably should be marketed to the R community a bit more. – eykanal Feb 29 '12 at 15:34

3 Answers3

3

You may try read.table

read.table("file.txt",sep="|")

lokheart
  • 23,743
  • 39
  • 98
  • 169
3

Like lokheart said, you'll want to use read.table. The documentation is pretty good... type ?read.table at the R command prompt to see help, there are examples at the end.

As for tutorials, there are a number of tutorials available online. You should check out this SO question for some excellent links, as well as the official R language introduction on the R website.

Community
  • 1
  • 1
eykanal
  • 26,437
  • 19
  • 82
  • 113
  • Thanks guys finally able to load the data using read.table and other parameters data<-read.table("fileName.dat",sep="|",header=FALSE,quote="",na.strings = "NA", colClasses = NA, nrows =-1,fill=TRUE,flush=TRUE) – pandhale Feb 29 '12 at 04:57
0

In R are several ways to load datasets in csv format, txt format or others formats, one way is using read.table for that you do the follow code:

setwd("Directorypath") ##remember use "//" if you are using a Windows OS

Will be something like that your directory path:

"C:\\firstfolder\\secondfolder"

If your database has a header, you can use the follow parameters to import your dataset:

dataset <- read.table("FileName", sep=",", header=T)

However, as I said, there are a bynch ways to import data in R. Another exemple is using read.csv

read.csv("FileName", sep=",", header=T)

Or use the readxl package to read Excel files:

read_excel("FileName", sheet="nameofsheet")

Although, the last way take more time to load as increase your database size, being recommended you use csv formats to process your data.

Arduin
  • 233
  • 4
  • 15