R, sink/cat: Output something else than numbers?


I'm rather new to R and I guess there's more than one thing inadequate practice in my code (like, using a for loop). I think in this example, it could be solved better with something from the apply-family, but I would have no idea how to do it in my original problem - so, if possible, please let the for-loop be a for-loop. If something else is bad, I'm happy to hear your opinion.

But my real problem is this. I have:

name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b") class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3") value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100) df <- data.frame(name, class, value) df

And i want to print it out. I use sink as well as hwriter (to output it as a html) later on. I get the problem with both, so I hope it's caused by the same and it's enough if we solve it for sink. That's the code:

sink("stuff.txt") for (i in 1:nrow(df)) { cat(" Name:") cat(df$name[i-1]) cat("\n") cat(" Class:") cat(df$class[i-1]) cat("\n") } sink() file.show("stuff.txt")

Part of the output I get is something like:

Name:1 Class:1 Name:1 Class:2 Name:1 Class:2

On the other hand, the output I want should be like:

Name:a Class:c1 Name:a Class:c2 Name:a Class:c2


The reason cat was printing numbers was that your character variables were converted to "factors" when you put them in the data.frame. This is the default behavior for data.frames. It is often a more efficient way to store the values because it converts each string value to a unique integer value. That's why you see numbers when you cat the value.

If you don't want to use factors in your data.frame, you can use

df <- data.frame(name, class, value, stringsAsFactors=F)

and this will keep the values as characters. Alternatively, you can convert to character when you print



  • Pandas conditional groupby count
  • How to swap out data via unload and load in C3.js
  • Compare Dictionary
  • PostgreSQL: update with left outer self join ignored
  • What is the difference in Angular2 between inject a provider in @Component and @Module?
  • Check if one value in one column is in another column
  • Range Multiplication VB.NET (What is wrong with this code?)
  • Tips for creating scalable WPF user control
  • JOOQ nested condition
  • Why is this button causing my layout to break?
  • iOS 6 dateFromString returns wrong date
  • in batch how do i use taskkill properly
  • Inversing an interpolation of rotation
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • For loop with if condition on multiple R functions
  • joining two bezier curves
  • Clear activity stack before launching another activity
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Implementation of State Monad
  • Breaking out column by groups in Pandas
  • wxPython: displaying multiple widgets in same frame
  • Refering to the class itself from within a class mehod in Objective C
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • R - Combining Columns to String Based on Logical Match
  • Symfony2: How to get request parameter
  • Weird JavaScript statement, what does it mean?
  • jquery mobile loadPage not working
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Java static initializers and reflection
  • embed rChart in Markdown
  • unknown Exception android
  • JaxB to read class hierarchy
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app