-2

I have a data-set that looks like this :

first <- c(5:14)
second <- rep(c("a","b"),5)
c <- cbind(first, second)
c
    first second  
 [1,] "5"  "a"
 [2,] "6"  "b"
 [3,] "7"  "a"
 [4,] "8"  "b"
 [5,] "9"  "a"
 [6,] "10" "b"
 [7,] "11" "a"
 [8,] "12" "b"
 [9,] "13" "a"
[10,] "14" "b"

As you can see, there are two levels (a and b)
I want to make a summary that shows what kind of values are in each level.

a : 5,7,9,11,13
b : 6,8,10,12,14
divibisan
  • 8,631
  • 11
  • 31
  • 46
kim1298
  • 21
  • 2
  • 9
  • something like `c[which(c[,2]=="a"),1]` `c[which(c[,2]=="b"),1]` – Niek Jun 21 '17 at 13:17
  • 3
    like `split(c[, "a"], c[, "b"])` ? (or just `split(a, b)` actually in your example case) – Cath Jun 21 '17 at 13:17
  • What do you mean by "multiple value for each level" ? Is good for you to have a list with 2 entries named "a","b" and each one of them with a vector of values ? – digEmAll Jun 21 '17 at 13:20
  • 1
    @lmo yes, works the same as using the columns' names, or just using the vectors `a` and `b` that were defined prior to the matrix `c` – Cath Jun 21 '17 at 13:22
  • 1
    to make this question clearer: you could indicate how the first column is relevant, or remove it; and could you use different column names vs. data values, or does it matter that they are both `a` and `b`? – Sam Firke Jun 21 '17 at 13:24
  • 2
    Don't use `cbind` here. Use `c – Roland Jun 21 '17 at 13:28
  • Other alternatives: by(first, second,c) or aggregate(first, list(second),c), where c is not Roland's definition but the function c() – skan Jun 21 '17 at 13:41
  • can be of help: https://stackoverflow.com/q/3505701/4137985 – Cath Jun 21 '17 at 13:43

1 Answers1

1

The OP has created a matrix with 2 columns. This needs to be converted to data.frame or data.table first before applying one of the many solutions found here.

# create matrix the way the OP has done it but using a different name 
# in order to avoid name conflicts with the c() function
first <- c(5:14)
second <- rep(c("a", "b"), 5)
mat <- cbind(first, second)

# one possible approach
library(data.table)
as.data.table(mat)[, .(first = toString(unique(first))), by = second]
   second            first
1:      a  5, 7, 9, 11, 13
2:      b 6, 8, 10, 12, 14

Note that the use of unique() was motivated by the OP's request to show what kind of values are in each level (emphasis mine).

Uwe
  • 34,565
  • 10
  • 75
  • 109
  • Alternately, if they want to do some analysis with the values instead of just browsing them, they could use `list` instead of `toString`, I guess. – Frank Jun 21 '17 at 16:06