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

China☆狼群 提交于 2019-12-24 07:18:43

问题


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!


回答1:


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.




回答2:


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)))
}))



回答3:


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
  }))



回答4:


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



来源:https://stackoverflow.com/questions/12927703/adding-a-space-between-two-characters-in-a-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!