作者aa955260 (aa955260)
看板R_Language
标题[问题] list问题
时间Thu Jan 12 01:03:05 2017
[问题类型]:
程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
[软体熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
各位大大好,我的资料集内有多个人(sub.list),每个人下面有3种不同状态(stat.list)
,每种状态下有3种不同的状况(level.list),目前我是用for-loop来分别读取2维的
data.frame(A, B, C),我想做的是用Scal.list将3种不同level.list的资料与名称包起
来,再用All.list把3种不同stat.list的资料与名称包起来,做成1个大的All.list。以
下为错误范例,请问我该怎麽修改会比较好呢?或是有没有其他更好的写法呢?
感谢
[程式范例]:
sub.list <- list("001", "002", "003")
stat.list <- list("High", "Low", "None")
level.list <- list("Light", "Moderate", "Vigorous")
Scal.list <- list()
All.list <- list()
for(i in 1:length(sub.list)){
for(j in 1:length(stat.list)){
A_path <- paste0(data.path, sub.list[i], "\\", stat.list[j]
,"\\", level.list[1], ".csv")
A <- fread(A_path, header = T)
B_path <- paste0(data.path, sub.list[i], "\\", stat.list[j]
, "\\", level.list[2], ".csv")
B <- fread(B_path , header = T)
C_path <- paste0(data.path, sub.list[i], "\\", stat.list[j]
, "\\", level.list[3], ".csv")
C <- fread(C_path, header = T)
t.list <- list()
t.list <- list(A, B, C)
names(t.list)[1] <- level.list[1]
names(t.list)[2] <- level.list[2]
names(t.list)[3] <- level.list[3]
Scal.list[[length(Scal.list)+1]] <- t.list
}
All.list[[length(All.list)+1]] <- Scal.list
}
[环境叙述]:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
(Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.3.2
[关键字]:
多层list、多层回圈读档
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.161.44.115
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1484154188.A.632.html
※ 编辑: aa955260 (1.161.44.115), 01/12/2017 01:04:17
1F:→ celestialgod: 三个list改用向量,配上expand.grid应该就可以直接 01/12 08:00
2F:→ celestialgod: 向量处理了 01/12 08:00
3F:→ aa955260: 不好意思,我有点不太理解,请问有范例code吗? 01/12 14:12