Sheila
Sheila

Reputation: 2596

Adding a space between two characters in a data frame in R

I have a data frame where each cell are 2 character strings (ie: "AA" , "BC" , "CD") where I am trying to put spaces between each of the two characters, and where NA values remain as is. I can't seem to figure this out. Any help????

Here is an example data frame:

 df <- data.frame(col1=c("AB", "CD", "EF"), col2=c("AA", "BB", "CC"), col3=c("XX", "YY", NA))

And this is what the example data frame looks like:

   col1 col2 col3
1   AB   AA   XX
2   CD   BB   YY
3   EF   CC <NA>

This is what i want my data frame to look like:

   col1  col2  col3
1   A B   A A   X X
2   C D   B B   Y Y
3   E F   C C   <NA>

Thanks in advance!

Upvotes: 1

Views: 7944

Answers (4)

psteelk
psteelk

Reputation: 1365

Did you try this ?

df <- data.frame(col1=c("A B", "C D", "E F"), col2=c("A A", "B B", "C C"), col3=c("X X", "Y Y", NA))

I tried and I'm getting what you require, Seems to be too silly !

If you are getting the column values dynamically, guess you can use a paste appropriately along with strsplit

Example

x <- "AB" 
strsplit(x, '')
[[1]]
[1] "A" "B"

Then you can use this and use paste appropriately

Upvotes: -2

Tyler Rinker
Tyler Rinker

Reputation: 109874

If it's as simple as you show this is an approach:

data.frame(lapply(df, function(x){
    ifelse(is.na(x), NA, 
    paste(substring(x, 1, 1), substring(x, 2)))
}))

Upvotes: 5

mnel
mnel

Reputation: 115392

If your data.frame columns are all factors, then you can work on the levels

as.data.frame(lapply(df, function(x){
 .l <- unlist(lapply(strsplit(levels(x),''), paste, collapse = ' '))
   levels(x) <- .l
 x}))

If your data.frame columns are character (stringsAsFactors = FALSE)

as.data.frame(lapply(df, function(x){
  .l <- unlist(lapply(strsplit(x,''), paste, collapse = ' '))
  .l
  }))

Upvotes: 4

BenBarnes
BenBarnes

Reputation: 19454

Here's one way

df2 <- data.frame(lapply(df, function(x) {
  levels(x) <- gsub("(.)(.)", "\\1 \\2", levels(x))
  return(x)
}))

df2

#   col1 col2 col3
# 1  A B  A A  X X
# 2  C D  B B  Y Y
# 3  E F  C C <NA>

This of course relies on the assumption that, when creating the data.frame df the argument stringsAsFactors is TRUE.

Upvotes: 3

Related Questions