简体   繁体   中英

How to assign a cat() value to an object in r

I have a character vector that I would like to process some functions on it.

vars <- c("1234_AS_SA1_PCNS","2345_AS_SA2_UDA", "3823_AS_SA3_CL")


cat(dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
                                          collapse=",\n ", sep=""), ";"), FALSE))

The above procedure prints this:

"Equal = (1234_AS_SA1_PCNS, Slope[0]),
 (2345_AS_SA2_UDA, Slope[0]),
 (3823_AS_SA3_CL, Slope[0]);"

My question is that how can I save this character variable in an object?

When I assigned this procedure to an object,

aa <- cat(dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
                                          collapse=",\n ", sep=""), ";"), FALSE))

I get aa NUll:

> aa
NULL

Any thoughts? Thanks!

cat doesn't have a return value. It just print . We could write the output to a file by specifying the file argument

cat(dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
                                      collapse=",\n ", sep=""), ";"), FALSE),
     file = 'file.txt')

The dQuote output can be assigned and not the cat wrapped on it

aa <- dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
                                      collapse=",\n ", sep=""), ";"), FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM