选择R中数据表中的列

时间:2018-04-09 18:38:34

标签: r data.table

我有"in_table",如下所示。我需要使用"Table1", "Table2", "Table3"获取"Comb_table"等等。基本上当Comb_table中的变量为1时,我需要在列表中包含。

在R语言中有没有有效的方法而不是手动输入所有组合?

感谢任何帮助。

感谢。

in_table:

POL    Var1  Var2  Var3  Var4  Var5    Var6    Var7 
8035   1     11    1     GRD   0030    0110    09/30
36763  1     88    13    GRD   5260    0300    11/15
36763  1     88    13    GRD   5280    0300    11/15
35786  1     88    13    GRD   0030    0110    09/30


Comb_table:
        Var1  Var2  Var3  Var4  Var5  Var6  Var7
 Table1   1     1   1     1     1     1     1
 Table2   0     1   1     1     1     1     1
 Table3   1     0   1     1     1     1     1


Table1 <- in_table[, .(Pol_count = length(unique(POL))), by = list(Var1,Var2,Var3,Var4,Var5,Var6,Var7)] 

Table2 <- in_table[, .(Pol_count = length(unique(POL))), by = list(Var2,Var3,Var4,Var5,Var6,Var7)] 

Table3 <- in_table[, .(Pol_count = length(unique(POL))), by = list(Var1,Var3,Var4,Var5,Var6,Var7)] 

and so on. 

3 个答案:

答案 0 :(得分:3)

type InnerSetToArray<T> = { 
    [P in keyof T]: 
        T[P] extends Set<infer U> 
            ? Array<U> 
            : T[P] 
};

type InnerSet = { one: number, two: Set<string>, three: Set<Function>, four: Date };

// InnerArray === { one: number, two: string[], three: Function[], four: Date }
type InnerArray = InnerSetToArray<InnerSet>;

答案 1 :(得分:2)

这有效:

> library(magrittr)
> melt(comb_table, id="tab", variable.factor=FALSE)[value == 1] %>% 
  split(by="tab") %>% 
  lapply(function(z) in_table[, .(n = uniqueN(POL)), by=c(z$variable)])

$Table1
   Var1 Var2 Var3 Var4 Var5 Var6  Var7 n
1:    1   11    1  GRD   30  110 09/30 1
2:    1   88   13  GRD 5260  300 11/15 1
3:    1   88   13  GRD 5280  300 11/15 1
4:    1   88   13  GRD   30  110 09/30 1

$Table3
   Var1 Var3 Var4 Var5 Var6  Var7 n
1:    1    1  GRD   30  110 09/30 1
2:    1   13  GRD 5260  300 11/15 1
3:    1   13  GRD 5280  300 11/15 1
4:    1   13  GRD   30  110 09/30 1

$Table2
   Var2 Var3 Var4 Var5 Var6  Var7 n
1:   11    1  GRD   30  110 09/30 1
2:   88   13  GRD 5260  300 11/15 1
3:   88   13  GRD 5280  300 11/15 1
4:   88   13  GRD   30  110 09/30 1

magrittr就是为了方便而在这里使用的。

或者,如果你把它全部放在一个表中并使用data.table&gt; = 1.10.5,那么这样的东西(我还没有测试过......)应该适用于分组集:< / p>

> melt(comb_table, id="tab", variable.factor=FALSE)[value == 1, groupingsets(
  in_table,
  sets = split(variable, tab)
)]

使用的数据:我认为OP的rownames是/应该是名为“tab”的列。

> dput(setDF(comb_table))
structure(list(tab = c("Table1", "Table2", "Table3"), Var1 = c(1L, 
0L, 1L), Var2 = c(1L, 1L, 0L), Var3 = c(1L, 1L, 1L), Var4 = c(1L, 
1L, 1L), Var5 = c(1L, 1L, 1L), Var6 = c(1L, 1L, 1L), Var7 = c(1L, 
1L, 1L)), .Names = c("tab", "Var1", "Var2", "Var3", "Var4", "Var5", 
"Var6", "Var7"), row.names = c(NA, -3L), class = "data.frame")
> dput(setDF(in_table))
structure(list(POL = c(8035L, 36763L, 36763L, 35786L), Var1 = c(1L, 
1L, 1L, 1L), Var2 = c(11L, 88L, 88L, 88L), Var3 = c(1L, 13L, 
13L, 13L), Var4 = c("GRD", "GRD", "GRD", "GRD"), Var5 = c(30L, 
5260L, 5280L, 30L), Var6 = c(110L, 300L, 300L, 110L), Var7 = c("09/30", 
"11/15", "11/15", "09/30")), .Names = c("POL", "Var1", "Var2", 
"Var3", "Var4", "Var5", "Var6", "Var7"), row.names = c(NA, -4L
), class = "data.frame")

答案 2 :(得分:0)

可能是这样的:

创建一个具有赋予1的变量名称的因子,并将NA赋予0

nm_list <- data.frame( do.call("rbind", Map( function(x,y) as.character(factor(x, levels = c(0,1), labels = c(NA, y))),
                                             x = Comb_table, y = names(Comb_table))),
                       stringsAsFactors = FALSE )
nm_list
#        X1   X2   X3
# Var1 Var1 <NA> Var1
# Var2 Var2 Var2 <NA>
# Var3 Var3 Var3 Var3
# Var4 Var4 Var4 Var4
# Var5 Var5 Var5 Var5
# Var6 Var6 Var6 Var6
# Var7 Var7 Var7 Var7

library('data.table')
setDT(in_table)  # convert data frame to data table by reference
lapply( nm_list, function(x) {
  x <- na.omit(x) # remove NA
  in_table[, .(Pol_count = length(unique(POL))), by = x]  # extract the variables by passing the values to by argument
})

# $X1
#    Var1 Var2 Var3 Var4 Var5 Var6  Var7 Pol_count
# 1:    1   11    1  GRD   30  110 09/30         1
# 2:    1   88   13  GRD 5260  300 11/15         1
# 3:    1   88   13  GRD 5280  300 11/15         1
# 4:    1   88   13  GRD   30  110 09/30         1
# 
# $X2
#    Var2 Var3 Var4 Var5 Var6  Var7 Pol_count
# 1:   11    1  GRD   30  110 09/30         1
# 2:   88   13  GRD 5260  300 11/15         1
# 3:   88   13  GRD 5280  300 11/15         1
# 4:   88   13  GRD   30  110 09/30         1
# 
# $X3
#    Var1 Var3 Var4 Var5 Var6  Var7 Pol_count
# 1:    1    1  GRD   30  110 09/30         1
# 2:    1   13  GRD 5260  300 11/15         1
# 3:    1   13  GRD 5280  300 11/15         1
# 4:    1   13  GRD   30  110 09/30         1

数据:

in_table <- read.table(text='POL    Var1  Var2  Var3  Var4  Var5    Var6    Var7 
8035   1     11    1     GRD   0030    0110    09/30
                       36763  1     88    13    GRD   5260    0300    11/15
                       36763  1     88    13    GRD   5280    0300    11/15
                       35786  1     88    13    GRD   0030    0110    09/30', header = TRUE)

Comb_table <- read.table(text = 'Var1  Var2  Var3  Var4  Var5  Var6  Var7
 Table1   1     1   1     1     1     1     1
                         Table2   0     1   1     1     1     1     1
                         Table3   1     0   1     1     1     1     1')