2

I am still relativly new to R and need some help on the following problem. I have da dataframe that looks smiliar to this (but way more complex)

Token1  A    B   E
Token2  A    F   D   G  
Token3  C    F   E
Token4  B    A   F

What I want is grouping each unique value that appears in one row so that one column only contains a value, if true for a row, and NA if not, like this:

Token1  A    B    NA   NA  E   NA  NA
Token2  A    NA   NA   D   NA  F   G  
Token3  NA   NA   C    NA  E   F   NA
Token4  A    B    NA   NA  NA  F   NA

I haven't found anything helpful so far...how do I get to the above result?

Thanks in advance!

Edit:

Thanks to all, but the actual DF is a lot more complex and contains several thousand possible values in all columns (I just used A, B, C etc. to simplify the problem), so the solutions won't seem to work... How do I group them all (I'm aware that there will be MANY single columns)?

4
  • 2
    I think in this case, it really matters how your data set looks like. Could you dputit?
    – rmuc8
    Apr 9, 2015 at 13:24
  • I tried to use dput(), but even for only n = 1, the output is HUGE (the dataframe has about 250 columns in total, if thats helpful) Apr 9, 2015 at 13:39
  • Use this: df <- data.frame(Token=c("Token1", "Token2", "Token3", "Token4"), Col1=c("A", "A", "C", "B"), Col2=c("B", "F", "F", "A"), Col3=c("E", "D", "E", "F"), Col4=c("", "G", "", "")) Apr 9, 2015 at 13:43
  • I had the structure of the empty cells in Col4 in mind, when I asked for the dput.
    – rmuc8
    Apr 9, 2015 at 13:59

3 Answers 3

1

You can try:

cols = unique(unlist(df[-1]))
cols = as.vector(sort(cols[!is.na(cols)]))

cbind(as.vector(df[,1]),
      t(apply(df[-1], 1, function(u) ifelse(cols %in% u[!is.na(u)], cols, NA))))
#  [,1]    [,2]     [,3]     [,4]      [,5]       [,6]      [,7]         [,8]      
#1 "cat"   NA       "dog.01" NA        NA         NA        NA           NA        
#2 "bird"  "cat.01" NA       NA        "robin.01" NA        NA           "eagle.01"
#3 "horse" NA       "dog.01" "pony.01" NA         NA        "unicorn.01" NA        
#4 "dog"   "cat.01" NA       NA        "robin.01" "bird.01" NA           NA        
#5 ""      NA       NA       NA        NA         NA        NA           NA        

Data:

df=structure(list(Lemma = structure(c(3L, 2L, 5L, 4L, 1L), .Label = c("", "bird", "cat", "dog", "horse"), class = "factor"), Sim = structure(c(3L, 5L, 4L, 2L, 1L), .Label = c("", "cat.01", "dog.01", "pony.01", "robin.01"), class = "factor"), X = structure(c(1L, 3L, 4L, 2L, 1L), .Label = c("", "bird.01", "cat.01", "unicorn.01"), class = "factor"), X.1 = structure(c(1L, 3L, 2L, 4L, 1L), .Label = c("", "dog.01", "eagle.01", "robin.01"), class = "factor")), .Names = c("Lemma", "Sim", "X", "X.1"), row.names = c(NA, 5L), class = "data.frame")
7
  • This is an excellent solution -- very minor addition you should consider: add cols = cols[order(cols)] before the cbind statement -- this will put the letters in order Apr 9, 2015 at 13:48
  • Thank you, but this works with the sample dataframe, but unfortunatly not with the original (very large) that I import with read.csv2, is there any way to apply it? Apr 9, 2015 at 14:07
  • Again to answer your question please provide dput(head(read.csv2(...))) Apr 9, 2015 at 15:14
  • Small sample: structure(list(Lemma = structure(c(3L, 2L, 5L, 4L, 1L), .Label = c("", "bird", "cat", "dog", "horse"), class = "factor"), Sim = structure(c(3L, 5L, 4L, 2L, 1L), .Label = c("", "cat.01", "dog.01", "pony.01", "robin.01"), class = "factor"), X = structure(c(1L, 3L, 4L, 2L, 1L), .Label = c("", "bird.01", "cat.01", "unicorn.01"), class = "factor"), X.1 = structure(c(1L, 3L, 2L, 4L, 1L), .Label = c("", "dog.01", "eagle.01", "robin.01"), class = "factor")), .Names = c("Lemma", "Sim", "X", "X.1"), row.names = c(NA, 5L), class = "data.frame") Apr 9, 2015 at 15:21
  • In this data.frame, what's the expected token column? The lemma column? Apr 9, 2015 at 15:24
1

I'm not sure why you would want such a structure, but you can try cSplit_e from my "splitstackshape" package after collapsing all of the non-token columns into a single string.

Here's an example with the sample data from Colonel Beauvel's answer.

df2 <- cbind(df[1], New = do.call(paste, c(df[-1], sep = ",")))
library(splitstackshape)

cSplit_e(df2, "New", ",", mode = "value", type = "character", drop = TRUE)
#   Lemma New_ New_bird.01 New_cat.01 New_dog.01 New_eagle.01 New_pony.01
# 1   cat             <NA>       <NA>     dog.01         <NA>        <NA>
# 2  bird <NA>        <NA>     cat.01       <NA>     eagle.01        <NA>
# 3 horse <NA>        <NA>       <NA>     dog.01         <NA>     pony.01
# 4   dog <NA>     bird.01     cat.01       <NA>         <NA>        <NA>
# 5                   <NA>       <NA>       <NA>         <NA>        <NA>
#   New_robin.01 New_unicorn.01
# 1         <NA>           <NA>
# 2     robin.01           <NA>
# 3         <NA>     unicorn.01
# 4     robin.01           <NA>
# 5         <NA>           <NA>

You'll have to just drop the "New_" column, which results from having some empty columns.

0

Here is another solution, for all letters:

df <- data.frame(Token=c("Token1", "Token2", "Token3", "Token4"),
       Col1=c("A", "A", "C", "B"), Col2=c("B", "F", "F", "A"), 
       Col3=c("E", "D", "E", "F"), Col4=c("", "G", "", ""))

df2 <- data.frame(df[, 1], t(sapply(1:dim(df)[1], function(i){
  toupper(letters) %in% c(t(df[i, -1]))
})))

names(df2) <- c("Token", toupper(letters))

for(i in 2:27){
  df2[, i][df2[, i]==T] <- names(df2)[i]
  df2[, i][df2[, i]==F] <- NA
}

df2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.