[R] Superscript and for loops
Hi, If I have data as follows: DATA_names-c( A mg kg B mg kg C mg kg D mg kg E mg kg F mg kg G mg kg H mg kg How do I convert to: -1 A (mg kg ) -1 B (mg kg ) -1 C (mg kg ) -1 D (mg kg ) -1 E (mg kg ) -1 F (mg kg ) -1 G (mg kg ) -1 H (mg kg ) I have lots of elements and I need to do this automatically in a for loop or the like? Thanks, Shane [[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.
Re: [R] Superscript and for loops
On Apr 3, 2013, at 9:06 AM, Shane Carey wrote: Hi, If I have data as follows: DATA_names-c( A mg kg B mg kg C mg kg D mg kg E mg kg F mg kg G mg kg H mg kg How do I convert to: -1 A (mg kg ) -1 B (mg kg ) -1 C (mg kg ) -1 D (mg kg ) -1 E (mg kg ) -1 F (mg kg ) -1 G (mg kg ) -1 H (mg kg ) I have lots of elements and I need to do this automatically in a for loop or the like? You haven't described the task in very much detail, so Bill Dunlap's language-oriented (more expressive as it were) solution my be what you really need. Nonetheless, this answer stays on the character-plane of R's conceptual levels: gsub(mg kg, (mg kg)^-1, DATA_names) [1] A (mg kg)^-1 B (mg kg)^-1 C (mg kg)^-1 D (mg kg)^-1 E (mg kg)^-1 F (mg kg)^-1 [7] G (mg kg)^-1 H (mg kg)^-1 If you wanted these in an expression vector suitable for labels on a barplot or such: sapply(gsub(mg kg, (mg kg)^-1, DATA_names), as.expression) expression(`A (mg kg)^-1` = A (mg kg)^-1, `B (mg kg)^-1` = B (mg kg)^-1, `C (mg kg)^-1` = C (mg kg)^-1, `D (mg kg)^-1` = D (mg kg)^-1, `E (mg kg)^-1` = E (mg kg)^-1, `F (mg kg)^-1` = F (mg kg)^-1, `G (mg kg)^-1` = G (mg kg)^-1, `H (mg kg)^-1` = H (mg kg)^-1) In practice: pos - barplot(1:length(DATA_names)) text(x=pos,y=-1, xpd=TRUE, srt=45, labels= sapply( gsub(mg kg, (mg kg)^-1, DATA_names), as.expression)) -- David Winsemius Alameda, CA, USA __ 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.
Re: [R] Superscript and for loops
Yup, I want these as labels on plots, but I need it as: D (mg kg^-1) rather than D (mg kg)^-1. Sorry for not being more clear and thanks for your help. Cheers On Wed, Apr 3, 2013 at 6:44 PM, David Winsemius dwinsem...@comcast.netwrote: On Apr 3, 2013, at 9:06 AM, Shane Carey wrote: Hi, If I have data as follows: DATA_names-c( A mg kg B mg kg C mg kg D mg kg E mg kg F mg kg G mg kg H mg kg How do I convert to: -1 A (mg kg ) -1 B (mg kg ) -1 C (mg kg ) -1 D (mg kg ) -1 E (mg kg ) -1 F (mg kg ) -1 G (mg kg ) -1 H (mg kg ) I have lots of elements and I need to do this automatically in a for loop or the like? You haven't described the task in very much detail, so Bill Dunlap's language-oriented (more expressive as it were) solution my be what you really need. Nonetheless, this answer stays on the character-plane of R's conceptual levels: gsub(mg kg, (mg kg)^-1, DATA_names) [1] A (mg kg)^-1 B (mg kg)^-1 C (mg kg)^-1 D (mg kg)^-1 E (mg kg)^-1 F (mg kg)^-1 [7] G (mg kg)^-1 H (mg kg)^-1 If you wanted these in an expression vector suitable for labels on a barplot or such: sapply(gsub(mg kg, (mg kg)^-1, DATA_names), as.expression) expression(`A (mg kg)^-1` = A (mg kg)^-1, `B (mg kg)^-1` = B (mg kg)^-1, `C (mg kg)^-1` = C (mg kg)^-1, `D (mg kg)^-1` = D (mg kg)^-1, `E (mg kg)^-1` = E (mg kg)^-1, `F (mg kg)^-1` = F (mg kg)^-1, `G (mg kg)^-1` = G (mg kg)^-1, `H (mg kg)^-1` = H (mg kg)^-1) In practice: pos - barplot(1:length(DATA_names)) text(x=pos,y=-1, xpd=TRUE, srt=45, labels= sapply( gsub(mg kg, (mg kg)^-1, DATA_names), as.expression)) -- David Winsemius Alameda, CA, USA -- Shane [[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.