I am trying to assign a comment to a data frame to store some relevant metadata. I have an unstructured text file wrapped in quote marks, with several line breaks ('\n').
WHO_comment<-read.table(file="WHO comment.txt", sep="\t")
comment(WHO)<-WHO_comment #Read in the comment from .txt due to its length
cat(comment(WHO)) #Database metadata
However, the readout comes in one large block with '\n' read as literal strings. Converting it with as.character() only returns the row name (i.e. '1').
How can I read in this file correctly?
read.table is the wrong function to read a text file. As the name suggests, its purpose is to read tabular data. To read a text file, use readLines, and then paste the individual lines together:
comment(data) = paste(readLines('WHO comment.txt'), collapse = '\n')
Solved it - I need to use stringsAsFactors=FALSE to read the file in correctly. This code now does what I wanted it to, which is assign a comment from a .txt file.
WHO_comment<-read.table(file="WHO comment.txt", sep="\t",stringsAsFactors=FALSE)
comment(WHO)<-WHO_comment #Read in the comment from .txt due to its length
cat(comment(WHO)) #Database metadata
Related
I am reading file in R:
data <- read.delim ("file.fas", header=TRUE, sep="\t" )
However, after I have done some manipulations to the data, the output format is not same. It now contains commas "," like this all over.
write.table(x= data, file = "file_1.fas")
How can I avoid this? Maybe I should use some different function to write a file?
I have an Excel with data, like this (but then N=1.000):
p_evar7_CO.main.
p_evar7_CP.acquistion..sign_up.start
p_evar7_CP.main.
p_evar7_CP.main.facial_stylers00
I want to put it in a vector, but with simple copy/pasting it goes wrong. I want this as result:
Excel <- c("p_evar7_CO.", "p_evar7_CP.acquistion..sign_up.start", "p_evar7_CP.main.","p_evar7_CP.main.facial_stylers00")
So basically: How can I paste a big data file into R, and automatically separate it with a Comma and Quote each row?
EDIT I don't want to load in an Excel data file, but only pasting columns names (and have them as a vector).
Looks like you could do a simple scan().
scan(file, what = "")
where file is your file name as a character string. If you are working with copied text, then you can enter "clipboard" as the file name.
scan("clipboard", what = "")
For example, I copied the file text from your question for the following code.
scan("clipboard", what="")
# Read 4 items
# [1] "p_evar7_CO.main." "p_evar7_CP.acquistion..sign_up.start"
# [3] "p_evar7_CP.main." "p_evar7_CP.main.facial_stylers00"
How to read csv files so that lines containing only commas are skipped?
Skip all leading empty lines in read.csv
covers couple ideas for the case that these lines are in the beginning of the file, but what about a more generic solution?
I am not sure about the performance but you can do this with the help of readLines(), grepl() and writeLines() function:
Assuming input file is A.csv and the output CSV file (though not necessary)which will not contain commas be B.csv.
test <- readLines('A.csv')
test2 <- test[!grepl(",",test)]
Either you can use test2 variable or save that to B.csv:
writeLines(test2,'B.csv')
I have a text file that I read like this:
file=read.table("file.txt",skip="1",sep="")
The first line of this text file contains information about the file then it is followed by the observations.
I want to extract the first line and write it out to a new text file.
To read the first line of a file, you can do:
con <- file("file.txt","r")
first_line <- readLines(con,n=1)
close(con)
To write it out, there are many options. Here is one:
cat(first_line,file="first_line.txt")
Another way to do it is to read it with read.table() like this:
read.table(file = 'file.txt',header = F,nrows = 1)
This is a simple way to do it, and you can get your data separated into columns which makes it easier to work with.
ItemID,Sentiment,SentimentSource,SentimentText
1,0,Sentiment140, ok thats it you win.
2,0,Sentiment140, i think mi bf is cheating on me!!! T_T
3,0,Sentiment140," I'm completely useless rt now. Funny, all I can do is twitter. "
How would you read a csv file like this into R?
Read a csv with read.csv(). You can specify sep="" to be whatever you need it to be. But as noted below, , is the default value for the separator.
R: Data Input
For example, csv file with comma as separator to a dataframe, manually choosing the file:
df <- read.csv(file.choose())