3

I am programming a classificator in R, I have cases that can have multiple classes, so I would like to have both classes in the same row and the column somthing$classes, as a vector. What I mean, is that the column something$classes, of the something data.frame should be like a Java or Python lists of lists. Here is an example of the entering data.frame data:

Case    class    class1     class2
  A       X                    Z
  B                 Y
  C       X         Y          Z
  D                 Y          Z

What I really need to do is to have class, class1 and class2 as one column named classes, with a vector as element, this is the data.frame I would like:

Case    classes  
A       [X, Z] %<- This is a vector, not an string      
B       [Y]          
C       [X, Y, Z]
D       [Y, Z]

Is there a way of having this data.frame structure? If so, how is done and how I could access an individual element inside each classes vector?

Thanks in advance

3
  • 3
    Data.frames support list columns: DF <- data.frame(a = 1:3); DF$b <- list(1:2, 2:3, 3:4) Commented Mar 15, 2017 at 8:45
  • So, I can use something like this in a bucle?: something$classes[i]=list(something$class[i], something$class1[i], something$class2[i]) But this will come in an error if I have some empty elements, like in case A, class1 is empty Commented Mar 15, 2017 at 8:52
  • 1
    Something in base R would be df$classes <- apply(df[-1], 1, function(x) list(x[x!=""])) Commented Mar 15, 2017 at 9:18

1 Answer 1

1

We can use data.table

library(data.table)
setDT(df1)[, {v1 <- unlist(.SD); list(classes = list(v1[v1!='']))}, Case]
Sign up to request clarification or add additional context in comments.

7 Comments

So, df1 is a new df, or the df I had? I tink I understand that when you do "classes =list(v1[v1!=''])" you are avoyding empty elements, but v1 what exactly is? are you using case as row selector, and the rest of the row should be V1? Thanks!
@Amnor df1 is the data.frame obect name. Grouped by 'Case', we are unlisting the other columns, remove the blank elements ("") and create a list from the vector elements
so df1, is the original object, this instruction, if I want my dataframe to be named "eclides" should be as usual: "euclides <- setDT(df1)[, {v1 <- unlist(.SD); list(classes = list(v1[v1!='']))}, Case]" Am I right? Thanks!
Thanks, going for it, just what I needed!!
It should be euclides$classes[[1]]
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.