Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 对列表的每个元素应用函数_R_List_Lapply - Fatal编程技术网

R 对列表的每个元素应用函数

R 对列表的每个元素应用函数,r,list,lapply,R,List,Lapply,我有以下几点 T<-as.data.frame(lapply(tables[3][[1]], function(y) gsub("\\s+", " ", y)),stringsAsFactors = FALSE) lapply(tables,function(x) as.data.frame(apply(x,2,function(y) gsub("\\s+", " ", y)))) 不必求助于笨重的for循环 谢谢 供参考(其他元素类似) 表[3] $`NULL` V1 V2 V3 V

我有以下几点

T<-as.data.frame(lapply(tables[3][[1]], function(y) gsub("\\s+", " ", y)),stringsAsFactors = FALSE)
lapply(tables,function(x) as.data.frame(apply(x,2,function(y) gsub("\\s+", " ", y))))
不必求助于笨重的for循环

谢谢

供参考(其他元素类似)

表[3]
$`NULL`
V1 V2 V3 V4
1值表示从有效到有效
2伤口
3 01A撕裂伤2010年7月1日
401B挫伤2010年7月1日
5 01C磨损2010年7月1日
6 01D软组织炎症\r\n 2010年7月1日
7 01Z伤口,其他或\r\n未指明2010年7月1日
8头部受伤\r\n
9 02A格拉斯哥昏迷评分\r\n 2010年7月1日15日

10 02B格拉斯哥昏迷评分\r\n您可以嵌套lapply以获得嵌套列表。但实际上你也可以使用一个for循环,就像HubertL在评论中提到的那样,这可能更容易阅读。 嵌套的lappy可以如下所示,其中1:length(tables)只提供表的索引

T<-lapply(1:length(tables), function(x){
                 as.data.frame(lapply(tables[x][[1]], function(y){
                   gsub("\\s+", " ", y)),stringsAsFactors = FALSE})
                 }
   )

T您可以嵌套lappy以获得嵌套列表。但实际上你也可以使用一个for循环,就像HubertL在评论中提到的那样,这可能更容易阅读。
嵌套的lappy可以如下所示,其中1:length(tables)只提供表的索引

T<-lapply(1:length(tables), function(x){
                 as.data.frame(lapply(tables[x][[1]], function(y){
                   gsub("\\s+", " ", y)),stringsAsFactors = FALSE})
                 }
   )

T尝试用以下内容重现您的数据

tables <- list()

tables[[1]] <- data.frame(V1=c("01D", "01Z", "02A"), V2=c("Soft tissue \r\n      inflammation ",
                                                      "Wound, other or \r\n      unspecified ", 
                                                      " Head \r\n      Injury                       "))

tables[[2]] <- data.frame(V1=c("01D", "01Z", "02A"), V2=c("Soft tissue \r\n      inflammation ",
                                                          "Wound, other or \r\n      unspecified ", 
                                                          " Head \r\n      Injury                       "))
内部apply函数将数据帧强制转换为矩阵,因此需要将其强制转换回外部lappy函数

输出

[[1]]
   V1                           V2
1 01D    Soft tissue inflammation 
2 01Z Wound, other or unspecified 
3 02A                 Head Injury 

[[2]]
   V1                           V2
1 01D    Soft tissue inflammation 
2 01Z Wound, other or unspecified 
3 02A                 Head Injury 

尝试用以下内容重现您的数据

tables <- list()

tables[[1]] <- data.frame(V1=c("01D", "01Z", "02A"), V2=c("Soft tissue \r\n      inflammation ",
                                                      "Wound, other or \r\n      unspecified ", 
                                                      " Head \r\n      Injury                       "))

tables[[2]] <- data.frame(V1=c("01D", "01Z", "02A"), V2=c("Soft tissue \r\n      inflammation ",
                                                          "Wound, other or \r\n      unspecified ", 
                                                          " Head \r\n      Injury                       "))
内部apply函数将数据帧强制转换为矩阵,因此需要将其强制转换回外部lappy函数

输出

[[1]]
   V1                           V2
1 01D    Soft tissue inflammation 
2 01Z Wound, other or unspecified 
3 02A                 Head Injury 

[[2]]
   V1                           V2
1 01D    Soft tissue inflammation 
2 01Z Wound, other or unspecified 
3 02A                 Head Injury 

for
循环可能是最容易理解的,如果它的相对速度不是问题,我认为您不应该牺牲代码的可读性。您能分享一个数据示例吗?例如使用dput。确实添加了列表中的一个元素。@brucezepplin我们需要一个与
而不是
表[3]
结构相同的示例。您是从
readHTMLTable()获得此列表的吗
偶然?for
循环的
可能是最容易理解的,如果相对缓慢不是问题,我认为您不应该牺牲代码的可读性。您能分享一个数据示例吗?例如,使用dput。您确实添加了列表中的一个元素。@brucezepplin我们需要一个与
而不是
表[3]
结构相同的示例。您是不是碰巧从
readHTMLTable()
获得此列表的?