Reproducible example:
adfcstable <- function(d, max = 5) {
d <- as.data.frame(d)
LevelADFtable <- matrix(, nrow = dim(d)[[2]]*3, ncol = 9) #18x9
FirstDiffADFtable <- matrix(, nrow = dim(d)[[2]]*3, ncol = 8) #18x8
Result <- matrix(, nrow = dim(d)[[2]]*3, ncol = 1) # 18x1
ADFtable <- cbind(LevelADFtable, FirstDiffADFtable, Result) # 18 x 18
colnames(ADFtable) <- c("var","type","levelt","Pc","c","Pt","t","prob","omlo","type","1stDifft","Pc","c","Pt","t","prob","omlo","result")
for (i in as.integer(1:dim(d)[[2]])) {
for (j in as.integer(1:3)) {
ADFtable[3*(i-1) + j,1] <- colnames(d)[[i]]
}
ADFtable[3*i-2,2] <- "dt"
ADFtable[3*i-2,10] <- "dt"
ADFtable[3*i-1,2] <- "d"
ADFtable[3*i-1,10] <- "d"
ADFtable[3*i,2] <- "-"
ADFtable[3*i,10] <- "-"
}
ADFtable
}
repexa <- as.data.frame(matrix(c(rnorm(10), rnorm(10), rnorm(10), rnorm(10), rnorm(10), rnorm(10)),ncol=6),stringsAsFactors = FALSE)
adfcstable(repexa)
# var type levelt Pc c Pt t prob omlo type 1stDifft Pc c Pt t prob
# [1,] "V1" "dt" NA NA NA NA NA NA NA "dt" NA NA NA NA NA NA
# [2,] "V1" "d" NA NA NA NA NA NA NA "d" NA NA NA NA NA NA
In var and type columns, there are values within quotation marks. I want the string values in the data frame without quotes.
What I did to solve the question:
I analyzed similar questions in SOF:
assigning a string to an object without double quotes
Remove quotes from a character vector in R
R: How to remove quotation marks in a vector of strings, but maintain vector format as to call each individual value?
But, they did not work in my example: I got the following errors in trying in those tricks:
1. # In code, I changed to this (but did not work):
ADFtable[3*i-2,2] <- noquote("dt")
2. # In code, I changed to this (but did not work):
# ADFtable[3*i-2,2] <- c("dt")
3. # In code, I changed to this (but gave error): # ADFtable[3*i-2,2] <- paste0("\n", "dt", "\n") adfcstable(repexa) Error in ADFtable[3 * i - 2, 2] <- cat(c("dt"), "\n") : number of items to replace is not a multiple of replacement length
# I analyzed the error:
cat(paste0("\n", "dt", "\n")) # dt
length(cat(paste0("\n", "dt", "\n"))) # 0
# Find the reason: Assigning a 0-lengthed value to 1-lengthed one.
4. print(adfcstable(repexa), quote=FALSE) worked a little: but it changed "NA" values to "< NA >" values in displaying the data frame.
What I want is (in displaying of the data frame as the returning value of the function):
adfcstable(repexa)
# var type levelt Pc c Pt t prob omlo type 1stDifft Pc c Pt t prob
# [1,] V1 dt NA NA NA NA NA NA NA dt NA NA NA NA NA NA
# [2,] V1 d NA NA NA NA NA NA NA d NA NA NA NA NA NA
Any help is greatly appreciated.