Changing x-Axis Values of graph [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My graph has on the y-axis numerical values of my data which is the level of depression and on the the x-axis I have order (numbers from 1-40 because I have 40 observations) But these are in fact quarters as my data is quarterly (2008-2013). So I would like to change the x-axis from an order of 1-40 to Year and Quarter (e.g. 2008 Q1,2008 Q2,..). I am however not sure how I can do that. Any help is greatly appreciated! Thank you for your Help!

You could make another column in your dataframe/matrix with the Year and Quarter.
Then set that column as the x-axis.
Something like this
for(i in 1:40){
if(i %% 4 == 0)
data$quarter[i] <- paste(as.integer((i - 1) / 4) + 2008, " Q4", sep = "")
else
data$quarter[i] <- paste(as.integer((i - 1) / 4) + 2008, " Q", i %% 4, sep = "")
}
This is of course assuming that they are in order. If they are not in order or you have overlap, you may have to just make it manually.

Related

New to R, I'm struggling to change the reference date of one of my variables to that of another date. Any help would be much appreciated [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
New to R, I'm struggling to change the reference date of one of my variables to that of another date. Any help would be much appreciated.
The current reference year is 2002 and I need to change the reference year so that it is 2015. Also, all my values in this column needs to be updated in accordance to the changed reference year. Thanks.
tsunami_data
colnames(tsunami_data)[1] <- "years_before"
Currently the years before are in relation to the date 2002 and I need to update to that of 2015, so that the years before become larger because of the shift in the date. Aslo, I am using a large data set at present. thanks.
e.g.
years_before
7
56
87
45
it is hard to understand what you want. Here I created a new variable and added the difference between 2002 and 2015.
library(dplyr)
df <- data_frame(years_before_2002 = c(7,56,87,45))
df_new <- df %>% mutate(years_before_2015 = years_before_2002 + (2015 - 2002))
#for your example
tsunami_data <- tsunami_data %>% mutate(years_before_2015 = years_before + (2015 - 2002))
#if you would like to keep "years_before", but just adjust for 13 years, run this
tsunami_data <- tsunami_data %>% mutate(years_before = years_before + (2015 - 2002))

Calculate Mean for Parts of time Series [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a data Frame of annual values that Looks something like this:
Time Value
01/2000-12/2000 123
01/2001-12/2001 126
01/2002-12/2002 129
...
01/2040-12/2040 223
I would like to Calculate the mean for certain parts of the time series (e.g. 2010-2015; 2015-2020; etc.)
Can anyone tell me how to do it?
# first extract the year
df$year <- as.numeric(sub(".*\\/", "", df$Time))
# then a simple mean() does the work for you!
mean(df$Value[df$year >= 2000 & df$year <= 2005])
You can do sth like this if your column Time is in Date format:
To transfer the column into date format use:
my.data.frame$Date = as.Date(paste("01.01.",sub(".*\\/", "", my.data.frame$Time),sep = ""),format = "%d.%m.%Y")
Then to calculate the mean:
mean(my.data.frame[my.data.frame$Date >= "2016-01-01" & my.data.frame$Date <= "2020-01-01","Value"])

conditionally selecting two numeric row values in R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to subset a data set to remove all values before the 7th month of the year 2011. I have Years and Months in different columns.
What I am doing I know is logically wrong(also getting a wrong output), but can't seem to figure out the right way to do this:
state_in2_check <- subset(state_in2, Month > 6 & Year > 2011)
#thelatemail has given you a workable solution in the comments. Your problem is that You're asking R to match two logical checks separately, but each of those checks is dependant on the other. You won't, for example, get any "January" dates (because you're only accepting months greater than 6), even though "Jan-2013" would be fine. #thelatemail's solution separates the checks, such that months lower than 6 will be accepted, as long as they're in years greater than 2011.
Another way would be to convert to date at the same time as subsetting, this way the process is a little more logical:
Month <- 7
Year <- 2011
as.Date( paste( Year, Month, 15, sep = "-" ) )
[1] "2011-07-15"
You can use that simple conversion to subset in a more (in my opinion) logical way:
state_in2_check <- subset(state_in2,
as.Date( paste( Year, Month, 15, sep = "-" ) ) >
as.Date( "2011-06-15" )
)
Note I've made the day of the month the same in both date conversions, which will mean they're compared only according to month/year.

I am using the data set "mhw.csv" from https://datahub.io/nl/dataset/mercer-and-hall-wheat-yield-data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am currently working with the data set "mhw.csv" , located at https://datahub.io/nl/dataset/mercer-and-hall-wheat-yield-dat
Which is a data frame pertaining
The data frame is separated into 4 columns:
"r" "c" "wheat" "straw"
Column r is a row number and c is a column number corresponding to an individual plot in the field. The field is 20 x 25. With a length of 500.
I want to divide the data into 4 quadrants, a NorthWest (rows 1:5 and Columns 1:12) NorthEast (rows 1:5 and columns 13:25) SouthWest (rows 5:10 and columns 1:12) SouthEast (rows 5:10 and columns 13:25)
Then add a 5th column to the data.frame that would denote where each of the plot is located.
Any help would be greatly appreciated. This is my first question, I hope I gave enough information.
Thank you!
I'm not going to go download that data, but using sample data:
test1 <- data.frame(r = sample(1:10, 10), c = sample(1:25, 10))
The simplest no-frills answer is probably:
test1$Quadrant[test1$r<=5 & test1$c<=12] <- "Northwest"
test1$Quadrant[test1$r>5 & test1$c<=12] <- "Southwest"
...
Et cetera. Do it for your four quadrants and the dataframe should now have the new column you're looking for.
PS: Generally you'll get quicker answers if you provide a sample dataframe like I did above with 'test1'.

categorizing date in R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working with a dataset in R where the main area of interest is the date. (It has to do with army skirmishes and the date of the skirmish is recorded). I wanted to check if these were more likely to happen in a given season, or near a holiday, etc, so I want to be able to see how many dates there are in the summer, winter, etc but I'm sort of at a loss for how to do that.
A general recommendation: use the package lubridate for converting from strings to dates if you're having trouble with that. use cut() to divide dates into ranges, like so:
someDates <- c( '1-1-2013',
'2-14-2013',
'3-5-2013',
'8-21-2013',
'9-15-2013',
'11-28-2013',
'12-22-2013')
cutpoints<- c('1-1-2013',# star of range 'winter'
'3-20-2013',# spring
'6-21-2013',# summer
'9-23-2013',# fall
'12-21-2013',# winter
'1-1-2014')# end of range
library(lubridate)
temp <- cut(mdy(someDates),
mdy(cutpoints),
labels=FALSE)
someSeasons <- c('winter',
'spring',
'summer',
'fall',
'winter')[temp]
Now use 'someSeasons' to group your data into date ranges with your favorite
statistical analysis. For a choice of statistical analysis, poisson
regression adjusting for exposure (i.e. length of the season), comes to
mind, but that is probably a better question for Cross Validated
You can make a vector of cut points with regular intervals like so:
cutpoints<- c('3-20-2013',# spring
'6-21-2013',# summer
'9-23-2013',# fall
'12-21-2013')# winter
temp <- cut(mdy(someDates),
outer(mdy(cutpoints), years(1:5),`+`),
labels=F)
someSeasons <- c('spring',
'summer',
'fall',
'winter')[(temp-1)%% 4 + 1] #the index is just a little tricky...

Resources