簡體   English   中英

表列表中的R新表

[英]R new table from list of tables

我有一個包含表列表的變量: list_of_tables :t1,t2,t3,t4,t5,t6等

list_of_tables (t1,t2,...)中的每個表都有8行。 例如

uuid | q_id | correct 
-----------------------
  1  | 1    |   T     
  1  | 2    |   T     
  1  | 3    |   F     
  1  | 4    |   F     
  1  | 5    |   T     
  1  | 6    |   F     
  1  | 7    |   F     
  1  | 8    |   T     

我想做的是從list_of_tables創建一個新表或數據框架,其中每行具有正確的分數,該分數基於正確== T的行數。

例如

uuid | c_score
--------------
  1  |  50% (4 out of 8 correct)
  2  |  ...
  3  |  ...

我將使用data.table,尤其是:

library(data.table)
dt1<-data.table(uuid=c(rep(1,5),rep(2,5)),c_score=c("T","F","F","F","T","T","T","T","F","F"))#mockup data

        uuid c_score
     1:    1       T
     2:    1       F
     3:    1       F
     4:    1       F
     5:    1       T
     6:    2       T
     7:    2       T
     8:    2       T
     9:    2       F
    10:    2       F

然后:

dt1[,sum(c_score=="T")/.N,by=uuid]#count the rows that are "T" in c_score and divide them by the total ones..

    uuid  V1
1:    1 0.4
2:    2 0.6

編輯:

如果是data.tables列表,例如

l1<-list(a=data.table(uuid=c(rep(1,5),rep(2,5)),c_score=c("T","F","F","F","T","T","T","T","F","F")),b=data.table(uuid=c(rep(1,5),rep(2,5)),c_score=c("T","T","F","T","T","F","F","F","T","T")))

您可以通過以下方式執行上述操作(前提是列名不變):

lapply(l1,function(x) x[,sum(c_score=="T")/.N,by=uuid])

yiedling:

    $a
       uuid  V1
    1:    1 0.4
    2:    2 0.6

    $b
       uuid  V1
    1:    1 0.8
    2:    2 0.4

這是一個R base解決方案:

# data
list_of_tables <- lapply(1:10,function(x)
 data.frame(uuid=rep(x,10),q_id=1:10,correct=sample(c(TRUE,FALSE),10,replace = T)))

> list_of_tables
[[1]]
   uuid q_id correct
1     1    1    TRUE
2     1    2   FALSE
3     1    3    TRUE
4     1    4    TRUE
5     1    5   FALSE
6     1    6   FALSE
7     1    7    TRUE
8     1    8   FALSE
9     1    9    TRUE
10    1   10    TRUE

[[2]]
   uuid q_id correct
1     2    1    TRUE
2     2    2   FALSE
3     2    3    TRUE
4     2    4   FALSE
5     2    5    TRUE
6     2    6    TRUE
7     2    7   FALSE
8     2    8    TRUE
9     2    9   FALSE
10    2   10   FALSE


new_t <- do.call(rbind,
                 lapply(list_of_tables,function(x) data.frame(uuid=unique(x$uuid),c_score = (sum(x$correct)/nrow(x))*100)))

在這種情況下, do.call將所有內容放回單個DF中,但是如果要保留列表,則可以跳過。

> new_t
   uuid c_score
1     1      60
2     2      50
3     3      80
4     4      70
5     5      70
6     6      40
7     7      60
8     8      50
9     9      50
10   10      50

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM