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!
Here’s one way
This of course relies on the assumption that, when creating the data.frame
dfthe argumentstringsAsFactorsisTRUE.