简体   繁体   中英

Create a string with special character in R

I have an issue creating a string with a special character. I have asked a similar question and I have also read answers to similar questions about my problem but I am not able to find the solution.

I want to create a string character with a special character. I have been trying with cat but I know it is only for printing, not for saving the string in a variable in R.

I want as a result this:

> cat("C:\\Users\\ppp\\ddd\\")
C:\Users\ppp\ddd\

and I have been trying with paste and collapse but without success:

> x = c("C:","Users","ppp","ddd")
> t <- paste0(x, collapse = '\n')
> t
[1] "C:\nUsers\nppp\nddd"

Are you sure you don't want

x = c("C:","Users","ppp","ddd")
t <- paste0(x, collapse = '/')
t
[1] "C:/Users/ppp/ddd"

R uses this format for setting working directories.

You can also do:

x = c("C:","Users","ppp","ddd")
t <- paste0(x, collapse = '\\')
t
[1] "C:\\Users\\ppp\\ddd"

Although this result looks wrong, if you are using the string in a shell() command in R to be interpreted Windows for example, it will be interpreted correctly

Not Answering... but

t <- paste0(x, collapse = '/')

"C:/Users/ppp/ddd" seems to work on windows.

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