Search code examples
rnames

List all column element names


Possibly it is an easy problem. I have a dataset like the following.

dat2 <- read.table(header=TRUE, text="
ID  De  Ep  Ti  ID1
A1123    A117 A121 A100 A11231
                   A1123 A108 C207 D110 E11232
                   A1124 A122 C207 D110 E11232
                   A1124 A117 C207 D110 E11232
                   A1124 A122 C208 D110 E11232
                   B1125 A108 C208 D110 E11232
                   B1125 A108 C208 D110 E11232
                   B1126 A122 C208 D110 E11233
                   C1126 A109 C208 D111 E11233
                   ")
dat2
dat2
     ID   De   Ep   Ti    ID1
1 A1123 A117 A121 A100 A11231
2 A1123 A108 C207 D110 E11232
3 A1124 A122 C207 D110 E11232
4 A1124 A117 C207 D110 E11232
5 A1124 A122 C208 D110 E11232
6 B1125 A108 C208 D110 E11232
7 B1125 A108 C208 D110 E11232
8 B1126 A122 C208 D110 E11233
9 C1126 A109 C208 D111 E11233

I can get summary by using table function.

table(dat2$ID)
A1123 A1124 B1125 C1126 
    2     3     3     1 

table(dat2$De)
A108 A109 A117 A122 
   3    1    2    3 

I want to list all element names in first two columns like the following.

t <- c("A1123", "A1124","B1125","C1126", "A108", "A109", "A117", "A122")
t
[1] "A1123" "A1124" "B1125" "C1126" "A108"  "A109"  "A117"  "A122"

Based on quick response from Neal, I can get the list of first two columns easily.

with(dat2, union(ID, De))
[1] "A1123" "A1124" "B1125" "C1126" "A117"  "A108"  "A122"  "A109" 

But if I want to get list of names from all columns, then the above is code not working.


Solution

  • use union to find unique values in two sets:

    with(dat2, union(ID, De))
    

    For all columns, use Reduce:

    Reduce(union, dat2)