Re: [R] Plotting lines to sets of points

2007-09-08 Thread Jim Price

# Create a matrix of ball locations
# You'd do this using the calls within your points function
balls - matrix(c(0,50,25,-150,-100,-50), ncol=2, byrow=F)


# Draw a line from the origin to each ball location
apply(balls, 1, function(x) lines(c(125, x[1]), c(-210, x[2]), col='red'))



A more complete example might loop over all the unique elements of
framename$hit_traj, and then run this procedure for each value with a
different colour, plotting both ball points and trajectories.



lawnboy34 wrote:
 
 I am using R to plot baseball spray charts from play-by-play data. I have
 used the following command to plot the diamond:
 
 plot (0:250, -250:0, type=n, bg=white)
   lines(c(125,150,125,100,125),c(-210,-180,-150,-180,-210), 
 col=c(black))
 
 I have also plotted different hit locations using commands such as the
 following:
 
 points(subset(framename$hit_x, framename$hit_traj==line_drive),
 subset(-framename$hit_y, framename$hit_traj==line_drive), pch=20,
 col=c(red))
 
 My question: Is there any easy way to plot a line from the origin (home
 plate) to each point on the graph? Preferably the line would share the
 same color as the dot that denotes where the ball landed. I have tried
 searching Google and these forums, and most graphing questions have to do
 with scatterplots or other varieties of graphs I am not using. Thanks very
 much in advance.
 
 -Jason
 

-- 
View this message in context: 
http://www.nabble.com/Plotting-lines-to-sets-of-points-tf4404235.html#a12564959
Sent from the R help mailing list archive at Nabble.com.

__
R-help@stat.math.ethz.ch 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] enable object name to be called as object (a dataset)

2007-09-08 Thread Jim Price

a - 1:3
b - 11:13
c - 21:23

names - c('a','b','c')

do.call(data.frame, list(sapply(names, function(x) get(x



runner wrote:
 
 What I am trying to do is as follows:
 
 - I have listed names of all wanted objects (datasets A,B,C... ) in
 current workspace as a vector: 
 
 obj - c('A','B','C')
 
 - then i need to use these objects, say to extract all the 1st columns and
 bind to an existing dataset ('data'): 
  
 for ( i in 1:3){
 newdata - obj[i] 
 data - cbind(data,newdata [[1]] )
 }
 
 Obviously, it doesn't work since obj[i] is just a string of dataset name.
 Here is my question: how to call it as a original dataset? Thanks.
 

-- 
View this message in context: 
http://www.nabble.com/enable-object-name-to-be-called-as-object-%28a-dataset%29-tf4403933.html#a12564175
Sent from the R help mailing list archive at Nabble.com.

__
R-help@stat.math.ethz.ch 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] Month end calculations

2007-08-30 Thread Jim Price

A simple example (avoiding using dates, just to show the principle) - this
assumes that your data are already sorted (?order).


temp - data.frame(subject = rep(1:2, each = 5), response = 1:10)
print(temp)

last - do.call(rbind, by(temp, temp$subject, function(x) tail(x, 1)))
print(last)


By changing the 2nd parameter to 'tail' you can get different numbers of
observations in the tail of each subset, so it has more power than SAS's
.last. If you need an equivalent of .first, then replace 'tail' with 'head'.

Jim.



Shubha Vishwanath Karanth wrote:
 
 Hi R users,
 
  
 
 Is there a function in R, which does some calculation only for the month
 end in a daily data?... In other words, is there a command in R,
 equivalent to last. function in SAS?
 
  
 
 BR, Shubha
 
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch 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.
 
 

-- 
View this message in context: 
http://www.nabble.com/Month-end-calculations-tf4347226.html#a12390874
Sent from the R help mailing list archive at Nabble.com.

__
R-help@stat.math.ethz.ch 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] summarizing dataframe at variable/factor levels

2007-07-05 Thread Jim Price

my.data - data.frame(
trts - rep(c('Drug 1','Drug2'), each = 10),
doses - rep(c('Low dose','High dose'), 10),
resp - rnorm(20)
)


tapply(my.data$resp, list(my.data$trts, my.data$doses), mean)




Jim





Afshartous, David wrote:
 
 
 All,
 
 Is there an efficient way to apply say mean or median to a dataframe
 
 according to say all combinations of two variables in the dataframe?
 Below is a simple example and the outline of a manual solution that
 will work but is not very efficient
 (could also generalize this to a function).  Searched the archives and
 docs but didn't see anything close to this question.
 
 Cheers,
 dave
 
 dat.ex = data.frame(  rep(c(1:6), each=6), c(rnorm(12), rnorm(12, 1),
 rnorm(12, 2)), rnorm(36, 5), rep(c(1:6), 6),
 rep(c(Drug1, Drug2, Placebo), each=12) )
 names(dat.ex) = c(patient.no, outcome, x, time, drug)
 
 mean of first 2 time pts on Drug1:
 mean.time.1.drug.1 = mean( dat.ex[dat.ex$time==1  dat.ex$drug==Drug1,
 c(2,3)])
 mean.time.2.drug.1 = mean( dat.ex[dat.ex$time==2  dat.ex$drug==Drug1,
 c(2,3)])
 
 dat.ex.reduced = as.data.frame(rbind(mean.time.1.drug.1,
 mean.time.2.drug.1))
 dat.ex.reduced$Drug = c(Drug1, Drug1)  ## add back Drug variable and
 time variable
 dat.ex.reduced$time = c(1,2)
 
 __
 R-help@stat.math.ethz.ch 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.
 
 

-- 
View this message in context: 
http://www.nabble.com/summarizing-dataframe-at-variable-factor-levels-tf4030788.html#a11450001
Sent from the R help mailing list archive at Nabble.com.

__
R-help@stat.math.ethz.ch 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.


[R] Changing graphics height when using grid and lattice

2007-06-28 Thread Jim Price

Hi,

I have recently been playing with the grid package in an attempt to create
some pages containing multiple lattice plots on the same page. However, when
I specify a grid layout with different widths, such as:

pushViewport(viewport(layout = grid.layout(1, 2, unit(c(2, 1), null

the individual graphs do not end up as the same height - which is a feature
I would prefer to have. 

A complete example is as follows:

### Start of example

library(lattice)
library(Hmisc)
library(grid)


# Incidence data
testData - data.frame(
strata = rep(c(CHF : Yes, CHF : No), each = 20),
ae = rep(paste(Adverse Event, 1:10), each = 2),
trt = rep(c(Active, Placebo), 20),
pct = runif(40, 1, 30)
)

# RR data
testData2 - data.frame(
strata = rep(c(CHF : Yes, CHF : No), each = 10),
ae = paste(Adverse Event, 1:10),
rr = runif(20, 0.5, 5)
)
testData2$lower = testData2$rr / 2
testData2$upper = testData2$rr * 2


# Combined plot
testPlots - function(relativeWidth)
{

plot1- dotplot(
ae ~ pct | strata, 
groups = trt, 
data = testData,  
layout = c(1, 2),
xlab = Percent,
auto.key = list(space = top, columns = 2)
)


plot2 - Dotplot(
ae ~ Cbind(rr, log10(lower), log10(upper)) | strata,
data = testData2,
panel = function(...)
{
panel.Dotplot(...)
panel.abline(v = 0, col = 'red', lty = 2)
},
layout = c(1, 2), 
scales = list(
x = list(log = T, at = c(0.125, 0.25, 0.5, 1, 2, 4, 8, 16, 32)),
y = list(draw = F)
),
xlab = Relative Risk with 95% CI,
ylab = ,
key = list(text = list())
)


grid.newpage()

pushViewport(viewport(layout = grid.layout(2, 1, heights = unit(c(1, 6),
null

pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1))
grid.text(Analysis of Relative Risks of various Adverse Events)
upViewport()

pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 2))


### Change the relative width of the 2 presented graphics
pushViewport(viewport(layout = grid.layout(1, 2, unit(c(relativeWidth, 1),
null

pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1))
print(plot1, newpage = F)
upViewport()

pushViewport(viewport(layout.pos.col = 2, layout.pos.row = 1))
print(plot2, newpage = F)
upViewport()

}


# Everything is fine, both graphs maintain the same y-axis
testPlots(1)

# The second graph is now taller than the first one
win.graph()
testPlots(3)

# End of example

I've been through the documentation of both lattice and grid, and I have not
been able to find the answer. I would appreciate any solution!

Regards,

James Price.
-- 
View this message in context: 
http://www.nabble.com/Changing-graphics-height-when-using-grid-and-lattice-tf3996724.html#a11350733
Sent from the R help mailing list archive at Nabble.com.

__
R-help@stat.math.ethz.ch 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.