pufftissue pufftissue
2008-Nov-20 04:59 UTC
[R] Calculating SD according to groups of rows
*Hi all, I know this is probably basic, but I have proven to be a slow learner in any programming language. Anyhow, how can I calculate the SD for each person in my table? I have two patients in this R data.frame, 7200 and 23955. I extracted this from a relational database, but am I better off attempting to compute SD in SQL, or is this easily accomplished in R? * SUBJECT_ID HR 1 7200 158 2 7200 165 3 7200 138 4 7200 152 5 7200 139 6 7200 157 7 7200 186 8 23955 167 9 23955 162 10 23955 171 11 23955 139 12 23955 170 13 23955 177 14 23955 180 15 23955 176 16 23955 172 17 23955 179 18 23955 181 19 23955 169 20 23955 168 21 23955 185 22 23955 181 23 23955 191 24 23955 179 25 23955 178 26 23955 184 27 23955 179 28 23955 172 29 23955 173 30 23955 182 31 23955 174 * So, what I would want is a table of 800 patients with a SD for their heart rates: subject id Heart Rate SD 7200 20 (for example) 23955 18 (for example)* Thank you! [[alternative HTML version deleted]]
Dear pufftissue, If your data set is a data.frame called 'x', one approach could be: # Data set x=read.table('clipboard',header=TRUE) # Calculations tapply(x$HR,x$SUBJECT_ID,sd,na.rm=TRUE) 7200 23955 16.39977 10.03896 See ?tapply and/or ?ave for more information. HTH, Jorge On Wed, Nov 19, 2008 at 11:59 PM, pufftissue pufftissue < pufftissue@gmail.com> wrote:> *Hi all, > > I know this is probably basic, but I have proven to be a slow learner in > any > programming language. Anyhow, > how can I calculate the SD for each person in my table? I have two > patients > in this R data.frame, 7200 and 23955. > I extracted this from a relational database, but am I better off attempting > to compute SD in SQL, or is this easily accomplished in R? > > > * SUBJECT_ID HR > 1 7200 158 > 2 7200 165 > 3 7200 138 > 4 7200 152 > 5 7200 139 > 6 7200 157 > 7 7200 186 > 8 23955 167 > 9 23955 162 > 10 23955 171 > 11 23955 139 > 12 23955 170 > 13 23955 177 > 14 23955 180 > 15 23955 176 > 16 23955 172 > 17 23955 179 > 18 23955 181 > 19 23955 169 > 20 23955 168 > 21 23955 185 > 22 23955 181 > 23 23955 191 > 24 23955 179 > 25 23955 178 > 26 23955 184 > 27 23955 179 > 28 23955 172 > 29 23955 173 > 30 23955 182 > 31 23955 174 > > * > So, what I would want is a table of 800 patients with a SD for their heart > rates: > > subject id Heart Rate SD > > 7200 20 (for example) > 23955 18 (for example)* > > Thank you! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
How about: with(dat, tapply(HR, SUBJECT_ID, sd)) Assuming your data frame is named dat. On Wed, 2008-11-19 at 23:59 -0500, pufftissue pufftissue wrote:> *Hi all, > > I know this is probably basic, but I have proven to be a slow learner in any > programming language. Anyhow, > how can I calculate the SD for each person in my table? I have two patients > in this R data.frame, 7200 and 23955. > I extracted this from a relational database, but am I better off attempting > to compute SD in SQL, or is this easily accomplished in R? > > > * SUBJECT_ID HR > 1 7200 158 > 2 7200 165 > 3 7200 138 > 4 7200 152 > 5 7200 139 > 6 7200 157 > 7 7200 186 > 8 23955 167 > 9 23955 162 > 10 23955 171 > 11 23955 139 > 12 23955 170 > 13 23955 177 > 14 23955 180 > 15 23955 176 > 16 23955 172 > 17 23955 179 > 18 23955 181 > 19 23955 169 > 20 23955 168 > 21 23955 185 > 22 23955 181 > 23 23955 191 > 24 23955 179 > 25 23955 178 > 26 23955 184 > 27 23955 179 > 28 23955 172 > 29 23955 173 > 30 23955 182 > 31 23955 174 > > * > So, what I would want is a table of 800 patients with a SD for their heart > rates: > > subject id Heart Rate SD > > 7200 20 (for example) > 23955 18 (for example)* > > Thank you! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Simon Blomberg, BSc (Hons), PhD, MAppStat. Lecturer and Consultant Statistician Faculty of Biological and Chemical Sciences The University of Queensland St. Lucia Queensland 4072 Australia Room 320 Goddard Building (8) T: +61 7 3365 2506 http://www.uq.edu.au/~uqsblomb email: S.Blomberg1_at_uq.edu.au Policies: 1. I will NOT analyse your data for you. 2. Your deadline is your problem. The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. - John Tukey.