My name is Natasa, I’m new in R. I’m impressed by what R can do, but unfortunately I don’t have the time to learn it from the beginning.
I have a lot of vectors (11) with 10000 values/numbers each, so I will be using a more “compact” version. Let’s say that I have 4 vectors: Where TI=Time, and RE= Region (1, 2 or 3).
TI -> c(10, 20, 30, 40, 50, 100, 150, 200, 300)
RE1 -> c(0.25, 0.78, 0.35, 0.37, 4.56, 5.23, 3.75, 8.51, 10.85)
RE2 -> c(0.05, 1.54, 0.4, 0.42, 2.53, 1.38, 4.58, 10.54, 25.35)
RE3 -> c(0.02, 0.53, 0.72, 0.28, 7.82, 13.51, 23.54, 2.15)
I want to create groups of “TI” (Time series: group1= TI corresponding to 10, 20, 30 and 40, group2= between 50-150 and group3= 200 and 300) and compute the mean and stdev for each RE vector according to /depending on the groups of TI. Each group is of unequal length and I don’t know the number of “variables” in each group (only the “range”). My final goal is to create a grouped bar plot for each group of TI and for each RE vector. In x axis there will be the groups of TI (the time series) and in y axis “values” of the regions, where in each time series there will be a separate “histogram” for each region.
I have found on the internet several pages and I have tried several things, but without any success. My thoughts were:
- To create a “table” (using the cbind function) like this: All -> cbind(TI, RE1, RE2, RE3)
- Partition the TI vector into groups and the other vectors according to the TI grouping. The pages that I have found are: Using the split function, as in: How to partition a vector into groups of neighbors in R? Split a vector into three vectors of unequal length in R or rename all the different values of TI according to the groups (group1, group2 and group3) using the replace function, like in: Replace given value in vector
- Use the aggregate function like in: Mean per group in a data.frame or R: how can I create a table with mean and sd according to experimental group alongside p-values?
- And finally use the barplot function.
The only problem is that I can’t found the correct way to split the table in the desired groups or in an “easy” way to rename specific values of TI (thought 2). Wanted table (If my "thoughts" are correct)
TI RE1 RE2 RE3
group1 0.25 0.05 0.02
group1 0.78 1.54 0.53
group1 0.35 0.4 0.72
group1 0.37 0.42 0.28
group2 4.56 2.53 7.82
group2 5.23 1.38 13.51
group2 3.75 4.58 23.54
group3 8.51 10.54 2.15
group3 10.85 25.35 0.65
Since my data is large, I don’t think that the replace function for each value is “affordable”. My other thought was to compute separately the mean and SD for each group of TI and RE and then to insert a column with the desire names of the group and then combine all the “tables” in one… but it will be very time consuming and not practical. Is there a way to “say” in R to rename all the numbers between 10-40 to group1, values between 50-150 to group2 etc. of the vector TI or that the numbers between… are a group etc.? If not, is there an easiest way to compute mean and sd for a specific range of values of a different vector? Or all those things aren’t needed and I can do it using the barplot function (I also tried to do it… without any success)?
It is really hard for me to figure it out with such limited experience, and any help will be greatly appreciated!! Thanks in advance for your responses.