访问R中的因子级别

时间:2021-05-20 22:53:55

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

我有一个5级因子,如下所示:

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:

我想访问除NA之外的每个级别中的项目。所以我使用了levels()函数,它给了我:

> 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!

你能帮我删除数字中的逗号和最后的数字(0)吗?我想有一个数字5,9,16,24,35,36,42的向量,以便我可以使用它们作为索引来访问数据帧。谢谢!

3 个解决方案

#1


2  

Does this do what you want?

这样做你想要的吗?

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

#2


3  

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

您需要使用unlist,strsplit和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:

接下来,在使用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" 

#3


0  

Using Andrie's dat

使用Andrie的数据

 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

#1


2  

Does this do what you want?

这样做你想要的吗?

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

#2


3  

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

您需要使用unlist,strsplit和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:

接下来,在使用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" 

#3


0  

Using Andrie's dat

使用Andrie的数据

 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