Hi! I am learning R and have a question that is probably fairly simple for
those of you much more learned than I.
I am messing with Arrays and am doing some simple stuff to get the hang of
them. I will have a seperate array already pulled up and it will have
columns and rows. I figured out that I can seperate these out with commands
like "array_name"[,1,1]. Now, I have an array where I want to make a single
array from a two ranges in the original array. from 1:7 and 12:34. I know
if i just did it with 1:7 it is just "array_name"[1:7,,] and that is it.
But i wanted 12:34 in there as well. I assumed at first that it would just
be "array_name"[1:7 & 12:34,,] but was wrong. Can anyone help me with this?
Thanks so much!
-B
Hi B,
What you need to do is pass a vector with the indices you want to
extract. So, you have 1:7 (which expands to 1, 2, 3, ... 7) and 12:34
(which again expands). How would you combine two sets of numbers?
c(), the combine or concatenate function. Putting this in action:
mya <- array(1:510, dim = c(34, 5, 3))
mya[c(1:7, 12:34), , ]
Do note that the row numbers will update unless they were explicitly
named. So they will be numbered 1:30, not 1:7 and 12:34.
Cheers,
Josh
On Sun, Nov 28, 2010 at 6:36 PM, bfhancock <brianfhancock at gmail.com> wrote:
Hi! I am learning R and have a question that is probably fairly simple for
those of you much more learned than I.
I am messing with Arrays and am doing some simple stuff to get the hang of
them. ?I will have a seperate array already pulled up and it will have
columns and rows. ?I figured out that I can seperate these out with commands
like "array_name"[,1,1]. ?Now, I have an array where I want to make a single
array from a two ranges in the original array. ?from 1:7 and 12:34. ?I know
if i just did it with 1:7 it is just "array_name"[1:7,,] and that is it.
But i wanted 12:34 in there as well. I assumed at first that it would just
be "array_name"[1:7 & 12:34,,] but was wrong. ?Can anyone help me with this?
Thanks so much!
-B
--
View this message in context: http://r.789695.n4.nabble.com/Array-help-tp3062992p3062992.html
Sent from the R help mailing list archive at Nabble.com.
Josh, the data set is called StatTemps and is in the PASWR package. I want
to make an array that involves only the 8 a.m. and a separate array that
involves only the 9 a.m. so i can get info on the temperatures in those
groups. So I still want it in the format of StatTemps but in two arrays that
are based on 8 a.m. or 9 a.m. I have been messing with this for a while.
And no it's not homework, I am just trying to learn R so I am more appealing
out in the field eventually. They book I am using is confusing! Hopefully
what I am trying to do isn't confusing. I want to do an array that holds
the info from 1:6 & 12:22 for 8am and then 7:11 & 23:34 for 9am. I was able
easily make two arrays based on sex by just putting in StatTemps[1:11,,] &
StatTemps[12:34,,] and assumed I could just go StatTemps[1:6&12:22,,] and
StatTemps[7:11&23:34,,] but that didn't work. Any ideas? Thanks so much!
-B
Josh, the data set is called StatTemps and is in the PASWR package.
I want
to make an array that involves only the 8 a.m. and a separate array
that
involves only the 9 a.m. so i can get info on the temperatures in
those
groups. So I still want it in the format of StatTemps but in two
arrays that
are based on 8 a.m. or 9 a.m. I have been messing with this for a
while.
And no it's not homework, I am just trying to learn R so I am more
appealing
out in the field eventually. They book I am using is confusing!
Hopefully
what I am trying to do isn't confusing. I want to do an array that
holds
the info from 1:6 & 12:22 for 8am and then 7:11 & 23:34 for 9am. I
was able
easily make two arrays based on sex by just putting in
StatTemps[1:11,,] &
StatTemps[12:34,,] and assumed I could just go
StatTemps[1:6&12:22,,] and
StatTemps[7:11&23:34,,] but that didn't work. Any ideas? Thanks so
much!
It may not be an array (since time values don't play very well with
that data structure.) If there is time as an index, it may be a more
complex object such as a time-series or zoo. Check with str().
Instead of:
7:11&23:34
I think you mean:
c(7:11, 23:34)
Using '&' for concatenation
is not an unreasonable idea,
but it is decidedly not what
R does.
It would be instructive to do:
7:11 & 23:34
at the R prompt to see what you
get.
On 29/11/2010 03:56, bfhancock wrote:
Josh, the data set is called StatTemps and is in the PASWR package. I want
to make an array that involves only the 8 a.m. and a separate array that
involves only the 9 a.m. so i can get info on the temperatures in those
groups. So I still want it in the format of StatTemps but in two arrays that
are based on 8 a.m. or 9 a.m. I have been messing with this for a while.
And no it's not homework, I am just trying to learn R so I am more appealing
out in the field eventually. They book I am using is confusing! Hopefully
what I am trying to do isn't confusing. I want to do an array that holds
the info from 1:6& 12:22 for 8am and then 7:11& 23:34 for 9am. I was able
easily make two arrays based on sex by just putting in StatTemps[1:11,,]&
StatTemps[12:34,,] and assumed I could just go StatTemps[1:6&12:22,,] and
StatTemps[7:11&23:34,,] but that didn't work. Any ideas? Thanks so much!
-B
if you can load the PASWR package and pull up StatTemps you will see what I
am talking about. Otherwise I fear that my question will just be confusing.
Hi Brian,
I believe there was some miscommunication earlier due to R's array
class for objects and the colloquial usage of array (the idea that
'array' is used colloquially is a bit odd, but I digress). In any
case, here are some steps I take (certainly not the only ones) when
exploring a new dataset that I am not familiar with:
## load the package
library(PASWR)
## look at the str()ucture of the object of interest
str(StatTemps)
## Hmm, it is a 'data.frame' with 3 variables
## one variable is 'num' and the other two are 'Factor'
## let's see if we can find out more about those data classes
## (pull up the documentation on each, it can be hard to know at first
## that 'num' stands for numeric and 'Factor' needs to be lowercase)
?data.frame
?numeric
?factor
## in this case, it is easy to print the whole data set so
StatTemps # print to screen
## but you can also get a nice little summary
summary(StatTemps)
## For the documentation on extraction/indexing
?Extract
## and some examples
StatTemps$temperature
StatTemps$gender
StatTemps$class
## now using a different operator than '$'
## You can call by name by quoting
StatTemps[ , "temperature"]
## or since we know it is column 1
StatTemps[ , 1]
## conversely, we can get row 1
StatTemps[1, ]
## or some combination of rows
StatTemps[c(1:7, 22:34), ]
## or rows and columns
StatTemps[c(1:7, 22:34), c(1, 3)]
## But since you have a factor, there may be an easier way
subset(StatTemps, gender == "Male")
subset(StatTemps, gender == "Female")
subset(StatTemps, class == "8 a.m.")
subset(StatTemps, class == "9 a.m.")
## on more than one variable
subset(StatTemps, class == "8 a.m." & gender == "Male")
## with a continuous variable
subset(StatTemps, temperature < 94)
## and we can do calculations by() groups
by(data = StatTemps$temperature, INDICES = StatTemps$gender, FUN = mean)
## but typing the name is annoying
with(StatTemps, by(data = temperature, INDICES = gender, FUN = mean))
## even more detailed (but leaving off the explicit argument names)
with(StatTemps, by(temperature, list(gender, class), mean))
## A couple visual summaries
boxplot(temperature ~ gender, data = StatTemps)
boxplot(temperature ~ class, data = StatTemps)
## or hop on over to lattice for something a little more advanced
bwplot(temperature ~ gender | class, data = StatTemps)
## and you can select certain parts without subset()
## first let's see what happens with
StatTemps$gender == "Female"
## now if you pass a logical vector to the extraction operator, '['
StatTemps[StatTemps$gender == "Female", ]
## same thing but just the first column
StatTemps[StatTemps$gender == "Female", 1]
## That came out as a vector, but
StatTemps[StatTemps$gender == "Female", 1, drop = FALSE]
HTH,
Josh
On Mon, Nov 29, 2010 at 5:01 AM, bfhancock <brianfhancock at gmail.com> wrote:
if you can load the PASWR package and pull up StatTemps you will see what I
am talking about. ?Otherwise I fear that my question will just be confusing.
--
View this message in context: http://r.789695.n4.nabble.com/Array-help-tp3062992p3063535.html
Sent from the R help mailing list archive at Nabble.com.
Thank you Joshua!! StatTemps[c(1:7, 22:34), ] that's what i needed! I
needed different ranges of rows but it is exactly what i was looking for.
Thanks so much!! R can be kind of fun! hehe