楼主: oliyiyi
1248 1

7 Important Ways to Summarise Data [推广有奖]

版主

已卖:2994份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
84105 个
通用积分
31671.0967
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

楼主
oliyiyi 发表于 2016-10-25 12:28:40 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
ntroduction

People remain confused when it comes to summarizing data real quick in R. There are various options. But, which one is the best ? I’ve answered this question below. You must choose one at first. And, become expert at it. That’s how you should move to the next.

People who transition from SAS or SQL are used to writing simple queries on these languages to summarize data sets. For such audience, the biggest concern is to how do we do the same thing on R.

In this article I will cover primary ways to summarize data sets. Hopefully this will make your journey much easier than it looks like.

Generally, summarizing data means finding statistical figures such as mean, median, box plot etc. If understand well with scatter plots & histogram, you can refer to guide on data visualization in R.


Methods to Summarise Data in R1. apply

Apply function returns a vector or array or list of values obtained by applying a function to either rows or columns. This is the simplest of all the function which can do this job. However this function is very specific to collapsing either row or column.

m <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2)apply(m, 1, mean)[1]  6  7  8  9 10 11 12 13 14 15apply(m, 2, mean)[1]  5.5 15.5
2. lapply

“lapply” returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.”

l <- list(a = 1:10, b = 11:20)lapply(l, mean)$a[1] 5.5$b[1] 15.5
3.  sapply

“sapply” does the same thing as apply but returns a vector or matrix. Let’s consider the last example again.

l <- list(a = 1:10, b = 11:20) l.mean <- sapply(l, mean)class(l.mean)[1] "numeric"
4. tapply

Till now, all the function we discussed cannot do what Sql can achieve. Here is a function which completes the palette for R. Usage is “tapply(X, INDEX, FUN = NULL, …, simplify = TRUE)”, where X is “an atomic object, typically a vector” and INDEX is “a list of factors, each of same length as X”. Here is an example which will make the usage clear.

attach(iris)# mean petal length by speciestapply(iris$Petal.Length, Species, mean)    setosa versicolor  virginica      1.462      4.260      5.552
5. by

Now comes a slightly more complicated algorithm. Function ‘by’ is an object-oriented wrapper for ‘tapply’ applied to data frames. Hopefully the example will make it more clear.

attach(iris)by(iris[, 1:4], Species, colMeans)Species: setosaSepal.Length  Sepal.Width Petal.Length  Petal.Width        5.006        3.428        1.462        0.246 ------------------------------------------------------------ Species: versicolorSepal.Length  Sepal.Width Petal.Length  Petal.Width        5.936        2.770        4.260        1.326 ------------------------------------------------------------ Species: virginicaSepal.Length  Sepal.Width Petal.Length  Petal.Width       6.588        2.974        5.552        2.026

What did the function do? It simply splits the data by a class variable, which in this case is the specie. And then it creates a summary at this level. So it does apply function on split frames. The returned object is of class “by”.


6. sqldf

If you found any of the above statements difficult, don’t panic. I bring you a life line which you can use anytime. Let’s fit in the SQL queries in R. Here is a way you can do the same.


attach(iris)summarization <- sqldf(select Species, mean(Petal.Length) from Petal.Length_mean where Species is not null group by Species’)And it’s done. Wasn’t it simple enough? One setback of this approach is the amount of time it takes to execute. In case you are interested in getting speed and same results read the next section.

7. ddplyFastest of all we discussed. You will need an additional package. Let’s do what we exactly did in tapply section.
library(plyr)attach(iris)# mean petal length by species

ddply(iris,"Species",summarise, Petal.Length_mean = mean (Petal.Length))


Additional Notes: You can also use packages such as dplyr, data.table to summarize data.  Here’s a complete tutorial on useful packages for data manipulation in R – Faster Data Manipulation with these 7 R Packages.

In general if you are trying to add this summarisation step in the middle of a process and need a table as output, you need to go for sqldf or ddply. “ddply” in these cases is faster but will not give you options beyond just grouping. “sqldf” has all features you need to summarize the data in SQL statements.

In case you are interested in using function similar to pivot tables or transposing the tables, you can consider using “reshape”. We have covered a few examples of the same in our article – comprehensive guide for data exploration in R.

Challenge : Here is a simple problem you can attempt to solve using all the methods we have discussed. You have a table for all school kids marks in a particular city.

Write a code to find the mean marks of each school for both class 1 and 2, for students with roll no less than 6. And print only the class whose mean score comes out to be higher for the school. For instance, if school A has a mean score of 6 for class 1 and 4 for class 2, you will reject class 2 and only take class 1 mean score for the school. In cases of tie, you can make a random choice. Assume that the actual table is much bigger and keep the code as generalized as possible.


End NotesYou can use any of the functions given above and get your results. I would recommend to execute the same task using all the functions discussed and check the time it takes to execute. Once you have this comparison in your mind, R will be as easy as SAS or and other SQL language.

Did you find the article useful? Have you used any other function to summarize data on R. If yes, tell us about the function and your views on other functions discussed in this blog.




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:IMPORTANT import Summa arise porta transition summarize audience question article

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

沙发
Kamize 学生认证  发表于 2016-10-25 12:47:02 来自手机
oliyiyi 发表于 2016-10-25 12:28
ntroductionPeople remain confused when it comes to summarizing data real quick in R. There are vario ...
谢谢楼主分享的资料不错了

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 11:54