简体   繁体   中英

apply function on every element of a list

I have the following

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

where each list element as a data frame.

As you can see this only works on the third element, but I want it to work on all elements.

How can I avoid doing something like this:

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)

without having to resort to a clunky for loop?

Thanks

for reference (other elements are similar)

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 

Trying to reproduce your data with the following.

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                       "))

I could manage the following

lapply(tables,function(x) as.data.frame(apply(x,2,function(y) gsub("\\s+", " ", y))))

The inner apply function coerces the data frame into a matrix so you need to coerce it back in the outer lapply function.

The output

[[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 

You could nest lapply to get a nested list. But actually you could also use a for-loop like mentioned by HubertL in the comments, which is maybe more clearly to read. A nested lapply could look like this, where 1:length(tables) gives just the index of the tables.

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

This is not a very sophisticated approach, but it should work properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM