[英]apply function on every element of a list
我有以下
T<-as.data.frame(lapply(tables[3][[1]], function(y) gsub("\\s+", " ", y)),stringsAsFactors = FALSE)
其中每个列表元素都作为一个数据帧。
如您所见,这仅适用于第三个元素,但我希望它适用于所有元素。
我如何避免做这样的事情:
as.data.frame(lapply(tables[1][[1]], function(y) gsub("\\s+", " ", y)),stringsAsFactors = FALSE)
as.data.frame(lapply(tables[2][[1]], function(y) gsub("\\s+", " ", y)),stringsAsFactors = FALSE)
as.data.frame(lapply(tables[3][[1]], function(y) gsub("\\s+", " ", y)),stringsAsFactors = FALSE)
不必诉诸笨拙的循环?
谢谢
供参考(其他元素相似)
tables[3]
$`NULL`
V1 V2 V3 V4
1 Value Meaning Valid From Valid To
2 Wound
3 01A Laceration 1st July 2010
4 01B Contusion 1st July 2010
5 01C Abrasion 1st July 2010
6 01D Soft tissue \r\n inflammation 1st July 2010
7 01Z Wound, other or \r\n unspecified 1st July 2010
8 Head \r\n Injury
9 02A Glasgow Coma Score \r\n 15 1st July 2010
10 02B Glasgow Coma Score \r\n <15 1st July 2010
11 02C Dental \r\nInjury 1st July 2010
12 02Z Head Injury, other or \r\n unspecified 1st July 2010
13 Fracture
14 03A Open \r\nFracture 1st July 2010
15 03B Closed \r\n Fracture 1st July 2010
16 03C Fracture \r\n Dislocation 1st July 2010
17 03Z Fracture, other or \r\n unspecified 1st July 2010
18 Joint \r\n Injury
19 04A Sprain 1st July 2010
20 04B Dislocation 1st July 2010
21 04C Subluxation 1st July 2010
22 04Z Joint Injury, other or \r\n unspecified 1st July 2010
23 Amputation
24 05Z Amputation, other or \r\n unspecified 1st July 2010
25 Soft Tissue \r\n Injury
26 06A Muscle \r\nInjury 1st July 2010
27 06B Tendon \r\nInjury 1st July 2010
28 06C Nerve Injury 1st July 2010
29 06D Visceral \r\n Injury 1st July 2010
30 06E Vascular \r\n Injury 1st July 2010
31 06Z Soft Tissue Injury, other \r\n or unspecified 1st July 2010
32 Burns, Scalds and \r\n Thermal Conditions
33 07A Electric 1st July 2010
34 07B Chemical 1st July 2010
35 07C Radiation 1st July 2010
36 07D Scald 1st July 2010
37 07E Sunburn 1st July 2010
38 07F Hyperthermia 1st July 2010
39 07G Hypothermia 1st July 2010
40 07H Frostbite 1st July 2010
41 07Z Burns, Scalds and Thermal \r\n Conditions, other or unspecified 1st July 2010
42 Foreign \r\n Body
43 08A Ingested Foreign \r\n Body 1st July 2010
44 08Z Foreign Body, other or \r\n unspecified 1st July 2010
45 Puncture \r\n Wounds
46 09A Needle Stick \r\n Injury 1st July 2010
47 09B Human Bite 1st July 2010
48 09C Animal Bite 1st July 2010
49 09D Insect Bite or \r\n Sting 1st July 2010
50 09Z Puncture Wounds, other or \r\n unspecified 1st July 2010
51 Poisoning or \r\n Overdose
52 10A Alcohol 1st July 2010
53 10B Prescribed \r\n Drug 1st July 2010
54 10C Non-prescribed/purchased \r\n drug 1st July 2010
55 10D Illicit \r\n Drug 1st July 2010
56 10Z Poisoning or Overdose, \r\n other or unspecified 1st July 2010
57 Drowning
58 11A Near \r\nDrowning 1st July 2010
59 11Z Drowning, other or \r\n unspecified 1st July 2010
60 Infectious \r\n Disease
61 12A Notifiable \r\n Disease 1st July 2010
62 12B Non-notifiable \r\n Disease 1st July 2010
63 Local \r\n Infection
64 13A Septicaemia 1st July 2010
65 13Z Infection, other or \r\n unspecified 1st July 2010
66 Respiratory \r\n Conditions
67 14A Asthma 1st July 2010
68 14B Chronic Obstructive \r\n Pulmonary disease 1st July 2010
69 14Z Respiratory Conditions, \r\n other or unspecified 1st July 2010
70 Endocrinological \r\n Conditions
71 15A Diabetes 1st July 2010
72 15Z Endocrinological \r\n Conditions, other or unspecified 1st July 2010
73 Cardiovascular \r\n Conditions
74 16A Myocardial \r\n Infarction 1st July 2010
75 16B Vascular Condition 1st July 2010
76 16Z Cardiovascular Conditions, \r\n other or unspecified 1st July 2010
77 Neurological Conditions
78 17A Seizure/Convulsion 1st July 2010
79 17B Cerebrovascular \r\n Event 1st July 2010
80 17Z Neurological Conditions, \r\n other or unspecified 1st July 2010
81 Gastrointestinal \r\n Conditions
82 18Z Gastrointestinal \r\n Conditions, other or unspecified 1st July 2010
83 Urological Conditions
84 19Z Urological Conditions, \r\n other or unspecified 1st July 2010
85 Dermatological \r\n Conditions
86 20Z Dermatological Conditions, \r\n other or unspecified 1st July 2010
87 Psychological/Psychiatric \r\n Conditions
88 21Z Psychological/Psychiatric \r\n Conditions, other or unspecified 1st July 2010
89 Obstetric \r\n Conditions
90 22Z Obstetric Conditions, \r\n other or unspecified 1st July 2010
91 Gynaecological \r\n Conditions
92 23Z Gynaecological Conditions, \r\n other or unspecified 1st July 2010
93 Haematological \r\n Conditions
94 24Z Haematological Conditions, \r\n other or unspecified 1st July 2010
95 Ophthalmic \r\n Conditions
96 25Z Ophthalmic Conditions, \r\n other or unspecified 1st July 2010
97 Rheumatological \r\n Conditions
98 26Z Rheumatological \r\n Conditions, other or unspecified 1st July 2010
99 Genito-Urinary \r\n Medicine
100 27Z Genito-urinary Medicine, \r\n other or unspecified 1st July 2010
101 Ear, Nose and Throat \r\n Conditions
102 28Z Ear, Nose and Throat \r\n Conditions, other or unspecified 1st July 2010
103 Pain
104 29A Chest Pain, non \r\n cardiac 1st July 2010
105 29B Abdominal \r\n Pain 1st July 2010
106 29Z Pain, other or \r\n unspecified 1st July 2010
107 Allergy (including \r\n Anaphylaxis)
108 30Z Allergy (including \r\n Anaphylaxis), other or unspecified 1st July 2010
109 Social \r\n Problems/Homelessness
110 31A Chronic Alcohol \r\n Abuse 1st July 2010
111 31B Chronic Drug \r\n Abuse 1st July 2010
112 31Z Social \r\n Problems/Homelessness, other or unspecified 1st July 2010
113 97Z Nothing Abnormal \r\n Detected 1st July 2010
114 98Z Diagnosis Type Not \r\n Otherwise Specified 1st July 2010
115 99Z Diagnosis Not \r\n Recorded 1st July 2010
尝试使用以下方法重现数据。
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 "))
我可以管理以下内容
lapply(tables,function(x) as.data.frame(apply(x,2,function(y) gsub("\\s+", " ", y))))
内部的apply函数将数据帧强制转换为矩阵,因此您需要在外部的lapply函数中将其强制返回。
输出
[[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
您可以套用嵌套以获取嵌套列表。 但是实际上,您也可以使用HubertL在评论中提到的for循环,可能更容易阅读。 嵌套的lapply可能看起来像这样,其中1:length(tables)仅给出表的索引。
T<-lapply(1:length(tables), function(x){
as.data.frame(lapply(tables[x][[1]], function(y){
gsub("\\s+", " ", y)),stringsAsFactors = FALSE})
}
)
这不是一个非常复杂的方法,但是它应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.