Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing a data.frame using cat

Tags:

dataframe

r

cat

How can I add/append data.frame abc to the text file that I have opened previously. I am writing some important information to that file and then I want to append that data.frame below that information. I get an error when I try to write the data.frame abc using cat.

fileConn<-file("metadata.txt","w+")
smoke <- matrix(c(51,43,22,92,28,21,68,22,9),ncol=3,byrow=TRUE)
smoke <- as.data.frame(smoke)
table <- sapply (smoke, class)
abc <- data.frame(nm = names(smoke), cl = sapply(unname(smoke), class))
cat("some imp info","\n", file=fileConn)
cat(abc,"\n", file=fileConn)
close(fileConn)
class(abc)
like image 962
user2543622 Avatar asked Aug 26 '14 16:08

user2543622


People also ask

What is the cat () function in R?

cat() function in R Language is used to print out to the screen or to a file.

What is the difference between cat and print?

print() returns a character vector. A vector is an object in R language. cat() returns an object NULL . cat() returns an object NULL .

Does cat return string?

The cat function is typically used to return character strings. The print function, however, is often also used for other data formats.

What is the structure of a data frame?

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.


1 Answers

Just use the standard tools for writing data.frame's, i.e. write.table:

write.table(abc, 'yourfile', append=TRUE) # plus whatever additional params
like image 176
eddi Avatar answered Oct 07 '22 04:10

eddi