简体   繁体   中英

Access the levels of a factor in R

I have a 5-level factor that looks like the following:

tmp

[1] NA                                                                   
[2] 1,2,3,6,11,12,13,18,20,21,22,26,29,33,40,43,46                       
[3] NA                                                                   
[4] NA                                                                   
[5] 5,9,16,24,35,36,42                                                   
[6] 4,7,10,14,15,17,19,23,25,27,28,30,31,32,34,37,38,41,44,45,47,48,49,50
[7] 8,39                                                                 
5 Levels: 1,2,3,6,11,12,13,18,20,21,22,26,29,33,40,43,46 ...

I want to access the items within each level except NA. So I use the levels() function, which gives me:

> levels(tmp)
[1] "1,2,3,6,11,12,13,18,20,21,22,26,29,33,40,43,46"                       
[2] "4,7,10,14,15,17,19,23,25,27,28,30,31,32,34,37,38,41,44,45,47,48,49,50"
[3] "5,9,16,24,35,36,42"                                                   
[4] "8,39"                                                                 
[5] "NA"      

Then I would like to access the elements in each level, and store them as numbers. However, for example,

>as.numeric(cat(levels(tmp)[3]))
5,9,16,24,35,36,42numeric(0)

Can you help me removing the commas within the numbers and the numeric(0) at the very end. I would like to have a vector of numerics 5, 9, 16, 24, 35, 36, 42 so that I can use them as indices to access a data frame. Thanks!

You need to use a combination of unlist , strsplit and unique .

First, recreate your data:

dat <- read.table(text="
NA                                                                   
1,2,3,6,11,12,13,18,20,21,22,26,29,33,40,43,46                       
NA                                                                   
NA                                                                   
5,9,16,24,35,36,42                                                   
4,7,10,14,15,17,19,23,25,27,28,30,31,32,34,37,38,41,44,45,47,48,49,50
8,39")$V1

Next, find all the unique levels, after using strsplit :

sort(unique(unlist(
  sapply(levels(dat), function(x)unlist(strsplit(x, split=",")))
  )))

 [1] "1"  "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "2"  "20" "21" "22" "23" "24" "25" "26"
[20] "27" "28" "29" "3"  "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "4"  "40" "41" "42" "43"
[39] "44" "45" "46" "47" "48" "49" "5"  "50" "6"  "7"  "8"  "9" 

Does this do what you want?

levels_split <- strsplit(levels(tmp), ",")
lapply(levels_split, as.numeric)

Using Andrie's dat

 val <- scan(text=levels(dat),sep=",")
 #Read 50 items

 split(val,cumsum(c(T,diff(val) <0)))
 #$`1`
 #[1]  1  2  3  6 11 12 13 18 20 21 22 26 29 33 40 43 46

 #$`2`
 #[1]  4  7 10 14 15 17 19 23 25 27 28 30 31 32 34 37 38 41 44 45 47 48 49 50

 #$`3`
 #[1]  5  9 16 24 35 36 42

 #$`4`
 #[1]  8 39

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM