Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在应用max.col后从每个列表级别检索行名称_R - Fatal编程技术网

R 如何在应用max.col后从每个列表级别检索行名称

R 如何在应用max.col后从每个列表级别检索行名称,r,R,下午好 假设我们有以下列表: L1=list(y1 = structure(c(2L, 2L, 5L, 3L, 3L, 4L), .Dim = 2:3, .Dimnames = structure(list( c("PURPLE", "YELLOW"), c("1", "2", "3")), .Names = c("", "")), class =

下午好

假设我们有以下列表:

L1=list(y1 = structure(c(2L, 2L, 5L, 3L, 3L, 4L), .Dim = 2:3, .Dimnames = structure(list(
    c("PURPLE", "YELLOW"), c("1", "2", "3")), .Names = c("", 
"")), class = "table"), y2 = structure(c(3L, 1L, 5L, 3L, 2L, 
5L), .Dim = 2:3, .Dimnames = structure(list(c("LARGE", "SMALL"
), c("1", "2", "3")), .Names = c("", "")), class = "table"), 
    y3 = structure(c(2L, 2L, 3L, 5L, 3L, 4L), .Dim = 2:3, .Dimnames = structure(list(
        c("DIP", "STRETCH"), c("1", "2", "3")), .Names = c("", 
    "")), class = "table"), y4 = structure(c(3L, 1L, 4L, 4L, 
    4L, 3L), .Dim = 2:3, .Dimnames = structure(list(c("ADULT", 
    "CHILD"), c("1", "2", "3")), .Names = c("", "")), class = "table"), 
    y5 = structure(c(3L, 1L, 5L, 3L, 4L, 3L), .Dim = 2:3, .Dimnames = structure(list(
        c("FALSE", "TRUE"), c("1", "2", "3")), .Names = c("", 
    "")), class = "table"))
#output
> L1
$y1
        
         1 2 3
  PURPLE 2 5 3
  YELLOW 2 3 4

$y2
       
        1 2 3
  LARGE 3 5 2
  SMALL 1 3 5

$y3
         
          1 2 3
  DIP     2 3 3
  STRETCH 2 5 4

$y4
       
        1 2 3
  ADULT 3 4 4
  CHILD 1 4 3

$y5
       
        1 2 3
  FALSE 3 5 4
  TRUE  1 3 3
对于每个变量$y,我尝试获取最频繁的模式(每个列)。对于每个列,我都尝试以最大频率检索行,如下所示:

lapply(res, function(x) max.col(t(x)))
$y1
[1] 2 1 2

$y2
[1] 1 1 2

$y3
[1] 1 2 2

$y4
[1] 1 1 1

$y5
[1] 1 1 1
现在,我需要获得相关的模式,例如:

    $y1
    [1] YELLOW PURPLE YELLOW
    $y2
    [1] LARGE LARGE SMALL
...
我尝试了但没有成功:
lappy(res,function(x)rownames(max.col(t(x)))

谢谢你的帮助

您必须使用索引对
rownames(x)
进行子集,而不是使用索引调用它,才能获得名称

lapply(L1, function(x) rownames(x)[max.col(t(x))])
#$y1
#[1] "PURPLE" "PURPLE" "YELLOW"
#
#$y2
#[1] "LARGE" "LARGE" "SMALL"
#
#$y3
#[1] "DIP"     "STRETCH" "STRETCH"
#
#$y4
#[1] "ADULT" "ADULT" "ADULT"
#
#$y5
#[1] "FALSE" "FALSE" "FALSE"