This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 1 year ago.
I have data table like this
year value
2010 25
2011 168
2012 48
2010 189
2011 192
2012 38
2010 175
2011 55
2012 48
I want to distinguish my data to be like this
year value
2010 3 (25 189 175: = 33.33% )
2011 3 (168 192 55 : = 33.33%)
2012 3 (48 38 48: = 33.33%)
for further plotting bar graph which have 3 main groups (2010 2011 2012) in X-axis and % of members in each year in Y-axis
What should I do? I'm a beginner in R program. Thank you in advanced :D
Do you want this
> data.frame(xtabs(~year, df))
year Freq
1 2010 3
2 2011 3
3 2012 3
or the plot
barplot(prop.table(xtabs(~year, df)))
You should note that for plotting the graph, the transformation you describe is not necessary. But you can do
dplyr::count(your_data, year)
returning
# A tibble: 3 x 2
year n
<dbl> <int>
1 2010 3
2 2011 3
3 2012 3
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 3 years ago.
Improve this question
I want to pipe my data table to frollmean to calculate rolling average of a column. But I am unable to get it work
head(mergedDT)
date Operating_hours DRIVING_TIME net_hrs workday
1 2018-03-20 110 759 0 TRUE
2 2018-03-21 121 641 11 TRUE
3 2018-03-22 133 625 12 TRUE
4 2018-03-23 145 672 12 TRUE
5 2018-03-24 145 0 0 FALSE
6 2018-03-25 145 0 0 FALSE
n_alarms
1 8
2 5
3 4
4 4
5 1
6 1
mergedDT %>% frollmean("n_alarms",2)
You can do:
mergedDT %>% mutate(mean=frollmean(n_alarms,2))
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 4 years ago.
Improve this question
I am trying to get the average sales for each week for each year, I have used :
weekavg <- aggregate(dod$Amount,list(dod$week),mean)
where dod is the name of my df as shown in the picture below.
The result gives me a list with just 53 rows ie for just 53 weeks - total for a single year.
How can I get the o/p such that it shows for each year ie 53 + 52 + 52 = 157 rows say for 3 years. I know my data is incomplete and does not entirely have 157 weeks, but I would like to know the logic so I can implement it. Any help would be appreciated.
dod:
Date Amount year month wday week wdayno
1 4/1/2015 38086.24 2015 4 Wednesday 13 3
2 4/2/2015 35426.65 2015 4 Thursday 14 4
3 4/3/2015 37170.98 2015 4 Friday 14 5
4 4/4/2015 36022.90 2015 4 Saturday 14 6
5 4/5/2015 28979.28 2015 4 Sunday 14 0
6 4/6/2015 28416.63 2015 4 Monday 14 1
7 4/7/2015 34945.63 2015 4 Tuesday 14 2
8 4/8/2015 17003.61 2015 4 Wednesday 14 3
9 4/9/2015 40903.74 2015 4 Thursday 15 4
10 4/10/2015 34091.52 2015 4 Friday 15 5
**]1
weekavg output :
..
...
42 42 36857.81
43 43 35191.18
44 44 37929.33
45 45 37601.31
46 46 37536.39
47 47 38021.44
48 48 38130.23
49 49 40730.46
50 50 35682.76
51 51 37400.04
52 52 38200.43
53 53 49619.85
We need to also have year in the grouping
aggregate(Amount~ week + year, data = dod, mean)
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 5 years ago.
Improve this question
I have 8 variables per company, with a total of 25 companies. However, i don't need to make any distinction between these companies. If you look at the example: I need to have AH and JUMBO in one column, the same for AHQ1 and JUMBOQ1, and for both Q2s. In this way i don't have 6 columns, but just 3 and twice as much observations in these rows. The title of the column can stay AH, AHQ1, and AHQ2.
Thanks in advance for any tips!!
Example of data:
df <- data.frame("ID" = c(1,1,2,2,2,2), "Year" = c(2012, 2015,2012,2013,2015,2016),
"AH" = c(1, NA, 1,1,1,1), "AHQ1" = c(8, NA,7,8,9,10),
"AHQ2" = c(10,NA,7,8,5,2),"JUMBO" = c(NA,NA,1,1,1,NA),
"JUMBOQ1" = c(NA,NA,8,9,7,NA), "JUMBOQ2"= c(NA,NA,10,9,7,NA))
temp <- cbind(df[1:2], df[6:8])
names(temp) <- names(df[1:5])
df2 <- rbind(df[1:5], temp)
> df2
ID Year AH AHQ1 AHQ2
1 1 2012 1 8 10
2 1 2015 NA NA NA
3 2 2012 1 7 7
4 2 2013 1 8 8
5 2 2015 1 9 5
6 2 2016 1 10 2
7 1 2012 NA NA NA
8 1 2015 NA NA NA
9 2 2012 1 8 10
10 2 2013 1 9 9
11 2 2015 1 7 7
12 2 2016 NA NA NA
Is this what you are looking for?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to calculate the mean and standard deviation for a variable within a subset. The coding works fine for mean but not sd. I have included sample where data= orf1 came from the subset. Any help?
mean(Stocking.Density2012,na.rm=TRUE,data=orf1)
[1] 13.72386
> sd(Stocking.Density2012,na.rm=TRUE,data=orf1)
Error in sd(Stocking.Density2012, na.rm = TRUE, data = orf1) :
unused argument (data = orf1)
Region Stocking.Density2012
1 12
8 7
2 12
8 17
1 34
3 24
1 16
2 5
1 5
4 11
1 5
3 3
7 3
5 13
1 18
4 15
2 18
1 10
6 5
1 10
5 46
1 19
3 12
1 15
6 4
1 4
7 8
1 8
8 12
data is neither an argument to mean nor to sd, so Stocking.Density2012 must be in the enclosing environment. Perhaps you attached it.
mean doesn't give an error because it has a ... argument, which sd does not.