r:data_transformations
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| r:data_transformations [2016/10/12 00:10] – hkimscil | r:data_transformations [2019/09/19 09:23] (current) – [Splitting a Vector into Groups] hkimscil | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| Warning message: | Warning message: | ||
| 패키지 ‘MASS’는 R 버전 3.2.5에서 작성되었습니다 | 패키지 ‘MASS’는 R 버전 3.2.5에서 작성되었습니다 | ||
| - | > split(Cars93$MPG.city, | + | > split(Cars93$MPG.city, |
| $USA | $USA | ||
| [1] 22 19 16 19 16 16 25 25 19 21 18 15 | [1] 22 19 16 19 16 16 25 25 19 21 18 15 | ||
| Line 39: | Line 39: | ||
| [1] 23.86667 | [1] 23.86667 | ||
| > | > | ||
| + | # or | ||
| + | > sapply(g, mean) | ||
| + | | ||
| + | 20.95833 23.86667 | ||
| + | # or retain list format | ||
| + | > lapply(g, mean) | ||
| + | $USA | ||
| + | [1] 20.95833 | ||
| + | |||
| + | $`non-USA` | ||
| + | [1] 23.86667 | ||
| + | |||
| + | |||
| </ | </ | ||
| ====== Applying a Function to Each List Element ====== | ====== Applying a Function to Each List Element ====== | ||
| Line 180: | Line 193: | ||
| < | < | ||
| + | suburbs <- read.csv(" | ||
| </ | </ | ||
| Line 203: | Line 217: | ||
| < | < | ||
| - | Cook | + | Cook |
| - | | + | |
| - | Will | + | |
| - | | + | > tapply(pop, |
| + | | ||
| + | 468852.3 147779.0 133134.5 106221.0 | ||
| </ | </ | ||
| + | The function given to tapply should expect a single argument: a vector containing all the members of one group. A good example is the length function, which takes a vector parameter and returns the vector’s length. Use it to count the number of data in each group; in this case, the number of cities in each county: | ||
| + | < | ||
| + | Cook | ||
| + | | ||
| + | </ | ||
| + | |||
| + | ====== Applying a Function to Groups of Rows ====== | ||
| + | < | ||
| + | dfrm = the data frame, | ||
| + | fact = grouping factor, | ||
| + | fun = function. The function should expect one argument, a data frame. | ||
| + | < | ||
| + | sel <- Cars93[c(" | ||
| + | |||
| + | > by(sel, sel$Orig, summary) | ||
| + | sel$Orig: USA | ||
| + | | ||
| + | | ||
| + | | ||
| + | Dodge : 6 Median : | ||
| + | Pontiac | ||
| + | Buick : 4 3rd Qu.: | ||
| + | Oldsmobile: 4 Max. : | ||
| + | (Other) | ||
| + | ------------------------------------------------------------------ | ||
| + | sel$Orig: non-USA | ||
| + | | ||
| + | | ||
| + | | ||
| + | Nissan | ||
| + | Toyota | ||
| + | Volkswagen: 4 3rd Qu.: | ||
| + | Honda : 3 Max. : | ||
| + | (Other) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | </ | ||
r/data_transformations.1476231020.txt.gz · Last modified: by hkimscil
