I have the following string
wrows <- "c(1:2,4)"
(which is read from a database, therefore not just c(1:2,4)).
I want to use this in an expression in the function read.xslx (openxlsx) like this
read.xlsx(inp_file,
sheetn,
rows = wrows,
cols = c(1,2))
)
How can I use wrows such that read.xlsx reads c(1:2,4) instead of "c(1:2,4)"?
Thanks
Renger
Related
cerveceria_dataset$CLIENTE <- separate(cerveceria_dataset$CLIENTE, col = CLIENTE, into = c("Nombre","Apellido"), sep = ";")
This code give me the
"Error in UseMethod("separate") :
no applicable method for 'separate' applied to an object of class "character""enter image description here
I think you are applying the function wrong. Try using -
cerveceria_dataset <- tidyr::separate(cerveceria_dataset,
col = CLIENTE, into = c("Nombre","Apellido"), sep = ";")
tidyr::separate splits a string column into multiple columns. If you are just working with a single character vector, you probably want something like stringr::split, which splits a character vector into a list of vectors (or a matrix if you use str_split_fixed).
I would like to run some queries with RStudio using the RODBC library. Normally, code like this works fine:
query_6 <- sqlQuery(con, "Select * from my_table where condition = more_than_sth")
I prefer to have some variable which will be defined by me before and stay for more_than_sth. Lets says it is x. Is there any method which would make me able to put this variable into the query string? Should I use some kind of paste, maybe before, or put it in directly?
Regards,
RafaĆ
Concatenate function in R is paste, it automatically append a whitespace between each object, you can remove them by using paste(..., sep = "") or paste0().
more_than_sth <- "x"
query_6 <- sqlQuery(con, paste0("Select * from my_table where condition ='", more_than_sth, "'"))
I'm sure this is simple, but I didn't find a solution.
I want to put my string called Data
Data
[1] "as.numeric(dataset$a),as.numeric(dataset1$a)"
in function data.frame to create a dataframe. I try:
DB<-data.frame(Data)
but the output is my string. If I call DB the output infact is:
Data
1 as.numeric(dataset$a),as.numeric(dataset1$a)
not the values into dataset$a, dataset1$a.
Thanks
Surely there is a better way to do whatever it is you want to do. But if you really want to run a string as if it were code you can use an eval(parse(text = string)) construction. However, it is generally a bad way to write code. Nonetheless here is a solution:
# a test dataframe
df = data.frame(a = 1:10, b = 11:20)
# string with code to run
string = "as.numeric(df$a),as.numeric(df$b)"
# split on , since those are separate lines of code
str = unlist(strsplit(string, ','))
# put it in a dataframe
df2 = data.frame(sapply(str, function(string) eval(parse(text = string))))
I wish to write the output from each loop iteration to a separate .csv file or add each loop as a new row in a separate object, which can them be output as a .csv, sequentially.
I've tried using write.csv but I am unsure how to apply it in the loop whereby a new file is created for each i.
for (i in seq(1,1128,12)){
Array <- ncvar_get(temp_cont, varid = "TREFHT", start = c(1,1,1,i), count= c(144,96,60,12))
myArray <- array(Array, dim =c(144, 96, 60, 12))
Annual <- apply(myArray, c(1,2,3), mean)
myArray2 <- array(Annual, dim = c(144,96,60))
Annuallat <- apply(myArray2, c(2,3), mean)
myArray3 <- array(Annuallat, dim = c(96,60))
AnnualGLobal <- apply(myArray3, 2,mean)
AnnualGlobalc <- AnnualGLobal - 273.15
write.csv(AnnualGlobalc, file = "year[i].csv")
}
Maybe this would give you an example / start:
write.csv(AnnualGlobalc, file = paste0(i,".csv"))
You can use paste() or paste0() to generate file name associated with loop index i on the fly.
Note I did not use paste0(year[i],".csv"), for two concerns:
I did not see variable year predefined;
You iterate i through seq(1,1128,12), which I sort of doubt whether year[i] is reasonable.
define a list to loop through... I then would write a function that does what i want... use the function within a call to lapply() ...
I have five dataframes (a-f), each of which has a column 'nq'. I want to find the max, min and average of the nq columns
classes <- c("a","b","c","d","e","f")
for (i in classes){
format(max(i$nq), scientific = TRUE)
format(min(i$nq), scientific = TRUE)
format(mean(i$nq), scientific = TRUE)
}
But the code is not working. Can you please help?
You can't use a character value as a data.frame name. The value "a" is not the same as the data.frame a.
You probably shouldn't have a bunch of data.frames lying around. You probably want to have them all in a list. Then you can lapply over them to get results.
mydata <- list(
a = data.frame(nq=runif(10)),
b = data.frame(nq=runif(10)),
c = data.frame(nq=runif(10)),
d = data.frame(nq=runif(10))
)
then you can do
lapply(mydata, function(x)
format(c(max(x$nq), min(x$nq), mean(x$nq)), scientific = TRUE)
)
to get all the values at once.
The reason it is not working is because 'i' is a character/string. As already mentioned by Mr.Flick you have to make it into a list.
Alternatively, you instead of writing i$nq in your loop you can write get(i)$nq. The get() function will search the workspace for an object by name and it will return the object itself. However, this is not as clean as making it into a list and using lapply.