Getting an error writing R function with gsub - r

Tried copying an old working code which substituted 2 values instead of 3. This isn't working though. Here it is:
Names <- c("Robert", "Mandy", "Mordecai")
Search <-function(find,relace,type){
gsub("find","relace",type)
}
Search("o","ooo", Names) #getting Names vector but no replacements

You need to remove the "'s from the line with the gsub call. Try:
gsub(find,relace,type)

Related

How to use loop function to rename multiple files' name after read them in R?

I have multiple files under the folder of "rawdata", after read them and assigned them as seperated dataset, i also want to rename them from "dataset 1 a.csv" to "dataset1".
I wrote the code that achieve the first goal,using the first loop read all files as a list, then use the second loop to unset the list: ldf. But I don't know where I should add the code to let R change all file's name at once? I tried to add str_replace_all (names(ldf)," ", "-") at different places, but all returned wrong outputs, and it cannot solve the issue of getting rid of ".csv". Thanks a lot~~
Here is my code:
datanames<-list.files(here("rawdata"))
ldf<-list()
for (i in (datanames)){
ldf[[i]]<-import(here("rawdata",i))
for (j in names(ldf)){
assign(j,ldf[[j]], .GlobalEnv)
}
}
I'm not sure the pattern of the name you want to replace, but if it is blank-number-blank-letter.csv, use gsub to remove. You then appear to want to add the index to the name, so paste0 with index i.
I'm not sure how you will import,but can use read.csv
Assign will assign the name.
lapply(1:length(list.files()), function(i) assign(paste0(gsub(" [0-9] [a-z].csv", "", list.files()[i]),i), read.csv(list.files()[i])))

What are the text delimiters for use in the R function paste()?

I am using some simple bash commands inside of an R for-loop to create some files with unique names.
Since the command arguments change with each iteration, I thought of using eval(parse(text=paste(system2(...)))), something like this:
index <- 1:5
for (i in index){
eval(parse(text=paste('system2("echo","`some text`>> myfile_',i,'.txt")',sep = "")))
}
However, to define the text string that is the argument to echo, I need a third delimiter because single, double and back quotes don't work. The above code with back quotes produces five lines of:
sh: some: command not found
while using the other quotes produces other errors.
Are there other delimiters that will work in this case?

Use LaF and grepl together

I would like to read in a possibly large text file and filter the relevant lines on the fly based on a regular expression. My first approach was using the package LaF which supports chunkwise reading and then grepl to filter. However, this seems not to work:
library(LaF)
fh <- laf_open_csv("myfile.txt", column_types="string", sep="°")
# would be nice to declare *no* separator
fh[grepl("abc", fh[[1]]), ]
returns an error in as.character.default(x) -- no method to convert this S4 to character. It seems like grepl is applied to the S4 function and not to the chunks.
Is there a nice way to read text lines from a large file and filter them selectively?
OK, I just discovered process_blocks:
regfilter <- function(df, result) c(result, df[grepl("1745", df[[1]]),1])
process_blocks(fh, regfilter)
This works, now I only need to find a way to ignore separators..

Using column/field names with parentheses in R

I am using R version 3.0.2 on Windows 7.
I load a CSV table into R and some of the column names have parentheses such as P(A) or P(A|B). If I try
whatever<- read.csv("C:/dir/name.csv", header=TRUE);
hist(whatever$P(A|B));
I get the error message
Error: unexpected symbol in "hist(whatever$P(A|B"
Is it possible, in R, to use column names with parentheses or must I change the column names to alphanumeric?
read.csv() will convert the special characters to '.' so the column 'P(A|B)' will be whatever$P.A.B..
However, as #Floo0 pointed out, if you can have column names like 'P(A|B)' which are accessed by whatever$"P(A|B)" or whatever[, "P(A|B)"].
Try
hist(whatever$"P(A|B)")
This should work fine.
Or use whatever[,i] where i is the number of column P(A|B)
Working example
whatever<-data.frame(test=rnorm(10))
colnames(whatever)<-"P(A|B)"
hist(whatever$"P(A|B)")

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

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.
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.
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.

Resources