0

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.

Community
  • 1
  • 1
Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40
  • 1
    It seems that your function returns a matrix, not a data.frame and that is probably the reason you see those quotes. The code that leads to matrix output in your function is `ADFtable <- cbind(LevelADFtable, FirstDiffADFtable, Result)` because you `cbind` several matrices. If you wrapped that in `as.data.frame` it should return a data.frame in the end (not tested, though). – talat Jan 03 '15 at 20:04
  • `""` is the way that `print` displays some missing values in particular classes. If you want R to behave different differently, you will need to define another class and a print method for it. Those are NOT the values in those vectors. – IRTFM Jan 03 '15 at 20:04
  • @BondedDust, not sure if your edit of the question title (changing from data.frame to matrix) represents the OP's intention. I think they believe it's a data.frame which it is not - so perhaps they want to know how to return it as a data.frame correctly? – talat Jan 03 '15 at 20:21
  • @BondedDust , Please re-change the title to reflect both the original title (data frame case) and current title (matrix case). Docendo's clever solution gets blurry with this new title. – Erdogan CEVHER Jan 03 '15 at 20:25
  • @ErdoganCEVHER, you can do that yourself as well. – talat Jan 03 '15 at 20:27
  • Original title of the question: How to remove quotes in string values in (displaying) a data frame (as a returning value of some function)? Proposed solutions replied even to the degree that: "When returning value of a function is data frame or matrix, how to remove quotes in string values within-function and out-of-function respectively?". Thx a lot to all the contributors. I over-satisfied to the level we reached. – Erdogan CEVHER Jan 03 '15 at 20:42
  • 1
    @ErdoganCEVHER: The original title incorrectly characterized the object you created. It was a matrix. You can change the code to create a data.frame or you can change the title back yourself. – IRTFM Jan 03 '15 at 20:50
  • @BondedDust, You are right. Perhaps, it is best to leave it as it is now. Thx for your careful and clear thinking and analysis. – Erdogan CEVHER Jan 03 '15 at 20:54

2 Answers2

1

This should work for that matrix object. See the ?print page:

print(adfcstable(repexa), quote=FALSE, na.print="NA")

I originally though that you had a factor column and if that were the case then this advice might have helped.: If you want the NA's to not be displayed as "<NA>" which is how print displays NA values in a factor vector), then you must prevent the data.frame methods from creating factors. This can be done at a global level with

options('stringsAsFactors'=FALSE)

Or you can put that argument in every call to read.table and its cousins and in all data.frame calls.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

(Solved by docendo discimus)

To employ docendo's trick, I added "ADFtable <- as.data.frame(ADFtable)" line in the code:

ADFtable <- cbind(LevelADFtable, FirstDiffADFtable, Result) # 18 x 18
ADFtable <- as.data.frame(ADFtable)

It resulted with no quotes then. Thx a lot, docendo.

Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40
  • Welcome. Note that you can also do it in one step (`as.data.frame(cbind(LevelADFtable, FirstDiffADFtable, Result))`). Also, there _might_ be more improvements you could make on your code but that's not the question here. – talat Jan 03 '15 at 20:24
  • 1
    It's necessary to warn you that there are many, many questions on StackOverflow and Rhelp that arise because people used as.data.frame(cbind(...)) and gotten unexpected results because of the silent coercion of character vectors to factors. – IRTFM Jan 03 '15 at 20:47
  • 1
    good point, @BondedDust. Should have written `as.data.frame(cbind(LevelADFtable, FirstDiffADFtable, Result), stringsAsFactors = FALSE)`. – talat Jan 03 '15 at 20:49