Maximilian
Maximilian

Reputation: 4229

Insert comma between characters with R

Sample data:

data <- round(rnorm(10,100000,10000),-1)
char <- as.character(data)

and I would like comma between the characters:

[1] "104350","108800","101320","94140","102260","89340","114220","105830","111270","85300"

I have tried paste() and also read.table & read.lines with sep="," with no success.

Upvotes: 5

Views: 8303

Answers (2)

botloggy
botloggy

Reputation: 403

The sink function can be used to force the text output.

sink('output.txt')

cat(paste0('"', paste(char, collapse="\", \""), '"'))

sink()

Upvotes: 0

Tyler Rinker
Tyler Rinker

Reputation: 109874

You're after the aesthetic not the functional with this answer:

cat(paste0('"', paste(char, collapse="\", \""), '"'))

## "115030", "118990", "72910", "121050", "93820", "119310", "110370", "100590", "98220", "118200"

To store:

store <- paste0('"', paste(char, collapse="\", \""), '"')

Upvotes: 5

Related Questions