[英]R new table from list of tables
I have a variable which contains a list of tables: list_of_tables
: t1, t2, t3, t4, t5, t6, etc 我有一个包含表列表的变量:
list_of_tables
:t1,t2,t3,t4,t5,t6等
Each table in list_of_tables
(t1, t2, ...) has 8 rows. list_of_tables
(t1,t2,...)中的每个表都有8行。 Eg 例如
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
What I would like to do is create a new table or data frame from list_of_tables
where each row has correct score, which is based on the number of rows where correct == T. 我想做的是从
list_of_tables
创建一个新表或数据框架,其中每行具有正确的分数,该分数基于正确== T的行数。
Eg 例如
uuid | c_score
--------------
1 | 50% (4 out of 8 correct)
2 | ...
3 | ...
I would use data.table and in particular: 我将使用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
Then: 然后:
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
In case of list of data.tables
such as 如果是
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")))
one can perform the above action (provided that the column names do not change) via: 您可以通过以下方式执行上述操作(前提是列名不变):
lapply(l1,function(x) x[,sum(c_score=="T")/.N,by=uuid])
yiedling: yiedling:
$a
uuid V1
1: 1 0.4
2: 2 0.6
$b
uuid V1
1: 1 0.8
2: 2 0.4
here's a R base
solution: 这是一个
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)))
In this case do.call
puts everything back into a single DF ... but you can skip that if you want to keep the lists. 在这种情况下,
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.