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"])
Related
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 need to calculate the % change in values for Argentina for the entire column and store it in a data frame:
% change is 2nd value- 1st value/ 1st value *100
say if a column has
30
40
%change is 40-30/30 =33.33%
Pls read abot how to make a reproducible example
Supposing df is your data.frame. Using dplyr:
library(dplyr)
df %>%
mutate(change = (Argentina - lead(Argentina)) / Argentina * 100
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.
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.
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...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to plot a graph for a data containing years between 1900 and 2010 and output in each month of the year in R. I need to select years between 1950-2001 against months of Nov-february. How can I select part of data for plotting this graph ?
since I am a rookie at R programming or any programming, an easy to follow example would be of great help.
thanks
GRV
I am not sure exactly what you mean by
select years between 1950-2001 against months of Nov-february
But the following should get you started on a reproducible example...
#create a vector of months from 1900 through 2010
months <- seq(as.Date("1900/1/1"), as.Date("2010/12/31"), "months")
#assign a random vector of equal length
output <- rnorm(length(months))
#assign both values to a data_frame
data <- data.frame(months = months, output = output)
Based on your description, your data should look something like the dataframe, called data.
From here, you can make use of the subset function to help you on your way. The first example subsets to data from 1950 through 2001. The next further restricts that subset to the months of November through February.
#subset to just 1950 through 2001
data_sub <- subset(data, months >= as.Date("1950-01-01") & months <= as.Date("2001-12-31"))
#subset the 1950 to 2001 data to just Nov-feb months (i.e. c(11,12,1,2))
data_sub_nf <- subset(data_sub, as.numeric(format(data_sub$months, "%m")) %in% c(11,12,1,2))
You should also read Why is `[` better than `subset`? to move beyond subset.
As stated, after the data has been subset, you can use plot or any other plotting function to graph your data.