Do you always use row.names=F in write.csv? Changing the default values inside R (base) functions
Asked Answered
R

2

10

Couldn't see a solution online but I thought this might be quite common.

  • with write.csv I basically always have the argument row.name set to F. Is it possible to run a line once and update the default value of the argument for the rest of the session?
  • I tried paste <- paste(sep="") which ran and returned no error but seemed to do nothing (and didn't destroy the paste function). This is another one, I always set sep="" with paste...
  • like I always have exclude=NULL when I am using table so I can see the N/A values.

EDIT: So, I'm looking for a solution that will work for multiple functions if possible: paste, write.csv, table and other functions like these.

Raptorial answered 11/7, 2011 at 3:18 Comment(1)
Just to mention it, for the case of paste, there's paste0 where sep = "" by default.Lahr
N
5

Try this:

paste <- paste
formals(paste)$sep <- ""

This creates a new copy of paste in your workspace, and then modifies its default value for sep to "". Subsequent calls to paste will then use the modified copy, as it sits in front of the base environment in your search path.

Nonreturnable answered 11/7, 2011 at 6:56 Comment(4)
Hmmm, this works for paste but not write.csv or table. Have updated question.Raptorial
@Raptorial For table it's tricky (to be clear for set NULL as default). Try formals(table)["exclude"] <- list(NULL). And write.csv is another story. I recommend to look at source of write.csv and write.csv2 and try to write write.csv3 to satisfy your needs.Ransome
Looking at the source to write.csv is likely to make you crySeigniory
@Anonymous_Downvotes: If you're going to down-vote, at least hint at what makes the answer unhelpful so others can learn and/or the answer can be improved.Parsifal
P
8

paste <- paste(sep="") puts the output of paste() into an object named "paste". You would need to do something like this instead.

paste <- function (..., sep = "", collapse = NULL) {
  base::paste(..., sep=sep, collapse=collapse)
}

You can also look at the Defaults package for this sort of thing, but it doesn't currently work for two of your examples.

Parsifal answered 11/7, 2011 at 3:55 Comment(8)
Hmm, I get a Error: evaluation nested too deeply: error with setDefaults(paste, sep=""). I'll have to look into that...Parsifal
Yeah, and the table part unfortunately doesn't work either. From the help for 'setDefaults' "Assigning NULL to any argument will remove the argument from the Defaults list."Raptorial
With regard to the first part of your answer, why did you do what you did with collapse?Raptorial
@nzcoops: I'm not sure what you mean by "why did you do what you did with collapse?" All I did was create a new paste function in the global environment (which is higher on the search path than package:base) with the same arguments as base::paste but with a different default sep argument.Parsifal
@joshua ...but in your example, you must call base::paste, not just paste! As it is written now, it causes infinite recursion...Haug
@Tommy: Thanks; good catch. I really should test the code in my answers or simply refrain from answering after midnight...Parsifal
@Joshua testing would be good ;) by the collapse question, I just mean did you have to do that? Or could you take out the collapse argument from both parts of that? Not being picky, just trying to understand :) I've updated the setDefaults part of your answer to since neither parts were working.Raptorial
You could remove the collapse argument but I'm not sure if doing so would cause issues with other functions that use paste. I'm working with the Defaults author to fix those issues. The code is 4 years old, so it's due for some updating. ;-)Parsifal
N
5

Try this:

paste <- paste
formals(paste)$sep <- ""

This creates a new copy of paste in your workspace, and then modifies its default value for sep to "". Subsequent calls to paste will then use the modified copy, as it sits in front of the base environment in your search path.

Nonreturnable answered 11/7, 2011 at 6:56 Comment(4)
Hmmm, this works for paste but not write.csv or table. Have updated question.Raptorial
@Raptorial For table it's tricky (to be clear for set NULL as default). Try formals(table)["exclude"] <- list(NULL). And write.csv is another story. I recommend to look at source of write.csv and write.csv2 and try to write write.csv3 to satisfy your needs.Ransome
Looking at the source to write.csv is likely to make you crySeigniory
@Anonymous_Downvotes: If you're going to down-vote, at least hint at what makes the answer unhelpful so others can learn and/or the answer can be improved.Parsifal

© 2022 - 2024 — McMap. All rights reserved.