Search code examples
rreplaceelementstrsplit

R Changing Elements in a Dataframe


I'm trying to, as the title says, change elements from my dataframe from one character to another. The dataframe is as follows:

g1=c("CC","DD","GG")
g2=c("AA","BB","EE")
g3=c("HH","II","JJ")

df=data.frame(g1,g2,g3)

I wish to convert the elements from letterletter format to letter/letter format (e.g. CC to C/C or AA to A/A)

I know using "strsplit" would work on a list. I also know that I would need to somehow incorporate: collapse="/"

How would I be able to apply the strsplit function to the entire dataframe?

I was thinking something along the lines of:

split=function(x)
{
  unlist(paste(strsplit(x,""),collapse="/"))
}

j=as.data.frame(apply(df,1,split))

but it doesn't give the desired results.

Update---------------- Apparently, the following script works:

split=function(x)
{
  paste(unlist(strsplit(x,"")),collapse="/")
}

p=apply(df,c(1,2),split)

If there is a more efficient or convenient way, please feel free to share.


Solution

  • I can think of two ways to approach this. One is using strsplit like you did. You were only missing the portion where you loop over each element in the list returned from strsplit:

    Split <- function(x) {
      #unlist(lapply(strsplit(x, ""), paste, collapse="/"))
      sapply(strsplit(x, ""), paste, collapse="/")
    }
    as.data.frame(lapply(df, Split))
    

    Another approach would be to use gsub and the \\B symbol, which matches the empty string that isn't at the beginning or end of a "word".

    as.data.frame(lapply(df, gsub, pattern="\\B", replacement="/"))
    

    What constitutes a "word" depends on locale and implementation, so here's another solution using gsub and back-references.

    as.data.frame(lapply(df, gsub, pattern="(.)(.)", replacement="\\1/\\2"))