My data has this format:
DF <- data.frame(ids = c("uniqueid1", "uniqueid1", "uniqueid1", "uniqueid2", "uniqueid2", "uniqueid2", "uniqueid2", "uniqueid3", "uniqueid3", "uniqueid3", "uniqueid4", "uniqueid4", "uniqueid4"), stock_year = c("April 2014", "March 2012", "April 2014", "January 2017", "January 2016", "January 2015", "January 2014", "November 2011", "November 2011", "December 2009", "August 2001", "July 2000", "May 1999"))
ids stock_year
1 uniqueid1 April 2014
2 uniqueid1 March 2012
3 uniqueid1 April 2014
4 uniqueid2 January 2017
5 uniqueid2 January 2016
6 uniqueid2 January 2015
7 uniqueid2 January 2014
8 uniqueid3 November 2011
9 uniqueid3 November 2011
10 uniqueid3 December 2009
11 uniqueid4 August 2001
12 uniqueid4 July 2000
13 uniqueid4 May 1999
How is it possible to remove totally rows which have in the same id have a same value in stock_year column?
An example output of expected results is this:
DF <- data.frame(ids = c("uniqueid2", "uniqueid2", "uniqueid2", "uniqueid2", "uniqueid4", "uniqueid4", "uniqueid4"), stock_year = c("January 2017", "January 2016", "January 2015", "January 2014", "August 2001", "July 2000", "May 1999"))
ids stock_year
1 uniqueid2 January 2017
2 uniqueid2 January 2016
3 uniqueid2 January 2015
4 uniqueid2 January 2014
5 uniqueid4 August 2001
6 uniqueid4 July 2000
7 uniqueid4 May 1999
We can group by 'ids' and check for duplicates to filter those 'ids' having no duplicates
library(dplyr)
DF %>%
group_by(ids) %>%
filter(!anyDuplicated(stock_year))
Or using ave from base R
DF[with(DF, ave(as.character(stock_year), ids, FUN=anyDuplicated)!=0),]
Related
I am new to R and am working on an assignment where I import some JSON data to (1) create a time series graph and (2) decompose the time-series. It's the decompose function where I'm struggling. Here is what works...
# Import JSON & convert to data.frame
aor <- fromJSON.....
aor <- as.data.frame......
# Combine the year and month into a date format
aor$date <- as.yearmon(paste(aor$year, aor$month), "%Y %m")
# Ensure data is float not chr
aor$mwh <- as.numeric(aor$mwh)
# Prep the data.frame for time-series analysis by converting to xts
aor <- xts(x = aor, order.by = aor$date)
# Successfully output a time-series graph.
dygraph(aor)
Here is a sample of aor up to this point...
> aor
mwh date
Jan 2001 " 1.42000" "Jan 2001"
Feb 2001 " 1.28400" "Feb 2001"
Mar 2001 " 1.25800" "Mar 2001"
Apr 2001 " 1.53600" "Apr 2001"
May 2001 " 1.47100" "May 2001"
Jun 2001 " 1.91800" "Jun 2001"
Jul 2001 " 2.37800" "Jul 2001"
> dput(head(aor, 10))
structure(c(" 1.42000", " 1.28400", " 1.25800", " 1.53600",
" 1.47100", " 1.91800", " 2.37800", " 2.47000", " 1.65100",
" 1.58100", "Jan 2001", "Feb 2001", "Mar 2001", "Apr 2001",
"May 2001", "Jun 2001", "Jul 2001", "Aug 2001", "Sep 2001", "Oct 2001"
), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("mwh", "date")))
The code I thought would produce the decomposition graphic...
ts <- as.ts(aor)
> ts
mwh date
Jan 1 1.42000 Jan 2001
Feb 1 1.28400 Feb 2001
Mar 1 1.25800 Mar 2001
Apr 1 1.53600 Apr 2001
May 1 1.47100 May 2001
Jun 1 1.91800 Jun 2001
Jul 1 2.37800 Jul 2001
d <- decompose(ts)
plot(d)
I get this error when trying to decompose ts...
Error in `-.default`(x, trend) : non-numeric argument to binary operator
I have a data frame
date df discharge cfs green discharge cfs north discharge cfs
1 December 2018 2520.1394 171.69667 338.81082
2 November 2018 3475.1023 239.00738 422.19063
3 October 2018 1863.4778 121.91720 200.94455
4 April 2019 3244.5356 260.38507 543.34792
5 August 2019 335.5074 14.95659 29.29938
6 February 2019 1631.3048 94.35956 198.19885
7 January 2019 1767.6266 132.69408 247.54493
8 July 2019 496.9439 26.37159 57.50114
9 June 2019 1097.2101 64.17292 143.40153
10 March 2019 1081.8046 80.32419 167.57954
11 May 2019 1507.8582 100.81569 236.58269
12 November 2019 2842.3542 284.72917 586.75000
13 October 2019 544.3002 34.67999 83.58193
14 September 2019 295.7200 11.37943 26.25823
and I want to change the "date" column into a 12-2018, 11-2018, ect. format like this:
date df discharge cfs green discharge cfs north discharge cfs
1 12-2018 2520.1394 171.69667 338.81082
2 11-2018 3475.1023 239.00738 422.19063
3 10-2018 1863.4778 121.91720 200.94455
4 04-2019 3244.5356 260.38507 543.34792
5 08-2019 335.5074 14.95659 29.29938
6 02-2019 1631.3048 94.35956 198.19885
7 01-2019 1767.6266 132.69408 247.54493
8 07-2019 496.9439 26.37159 57.50114
9 06-2019 1097.2101 64.17292 143.40153
10 03-2019 1081.8046 80.32419 167.57954
11 05-2019 1507.8582 100.81569 236.58269
12 11-2019 2842.3542 284.72917 586.75000
13 10-2019 544.3002 34.67999 83.58193
14 09-2019 295.7200 11.37943 26.25823
Currently the "date" column is in "character" format. How can I change this to a date or POSIXct format and so that it looks like it does above? Thanks.
We can use as.yearmon to convert to yearmon class and then change the format
library(zoo)
df1$date <- format(as.yearmon(df1$date, "%B %Y"), "%m-%Y")
df1$date
#[1] "12-2018" "11-2018" "10-2018" "04-2019" "08-2019" "02-2019" "01-2019" "07-2019" "06-2019" "03-2019" "05-2019" "11-2019" "10-2019"
#[14] "09-2019"
data
df1 <- structure(list(date = c("December 2018", "November 2018", "October 2018",
"April 2019", "August 2019", "February 2019", "January 2019",
"July 2019", "June 2019", "March 2019", "May 2019", "November 2019",
"October 2019", "September 2019"), df_discharge_cfs = c(2520.1394,
3475.1023, 1863.4778, 3244.5356, 335.5074, 1631.3048, 1767.6266,
496.9439, 1097.2101, 1081.8046, 1507.8582, 2842.3542, 544.3002,
295.72), green_discharge_cfs = c(171.69667, 239.00738, 121.9172,
260.38507, 14.95659, 94.35956, 132.69408, 26.37159, 64.17292,
80.32419, 100.81569, 284.72917, 34.67999, 11.37943),
north_discharge_cfs = c(338.81082,
422.19063, 200.94455, 543.34792, 29.29938, 198.19885, 247.54493,
57.50114, 143.40153, 167.57954, 236.58269, 586.75, 83.58193,
26.25823)), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"))
In base R, we can paste an arbitrary date , convert to date object and then format
format(as.Date(paste0('1', df$date), '%d %B %Y'), '%m-%Y')
Another option with regex and using an inbuilt vector month.name can be
with(df, paste(match(sub('\\s\\d+', '', date), month.name),
sub('.*\\s+', '', df$date), sep = '-'))
edit: See solution at the bottom.
I have trouble using the outcome of an which.max outcome in a list.
Below follows an example which reproduces my problem.
Create dataframe
library(dplyr)
library(ggplot2)
library(forcats)
name <- c('A','A','A', 'A','A','A', 'A','A','A',
'B','B','B', 'B','B','B', 'B','B','B',
'C','C','C', 'C','C','C', 'C','C','C')
month = c("oct 2018", "oct 2018", "oct 2018","nov 2018", "nov 2018", "nov 2018","dec 2018", "dec 2018", "dec 2018",
"oct 2018", "oct 2018", "oct 2018","nov 2018", "nov 2018", "nov 2018","dec 2018", "dec 2018", "dec 2018" ,
"oct 2018", "oct 2018", "oct 2018","nov 2018", "nov 2018", "nov 2018","dec 2018", "dec 2018", "dec 2018" )
value <- seq(1:length(month))
df = data.frame(name, month, value)
df
Outcome
name month value
A oct 2018 1
A oct 2018 2
A oct 2018 3
A nov 2018 4
A nov 2018 5
A nov 2018 6
A dec 2018 7
A dec 2018 8
A dec 2018 9
B oct 2018 10
B oct 2018 11
B oct 2018 12
B nov 2018 13
B nov 2018 14
B nov 2018 15
B dec 2018 16
B dec 2018 17
B dec 2018 18
C oct 2018 19
C oct 2018 20
C oct 2018 21
C nov 2018 22
C nov 2018 23
C nov 2018 24
C dec 2018 25
C dec 2018 26
C dec 2018 27
Extract name of the observation with the largest value
memberLargestValue = df[which.max(df$value),]$name
memberLargestValue
Outcome
[1] C
Levels: A B C
Merge memberLargestValue with pre-existing list
oldList = c("A", "A")
newList = c(oldList, memberLargestValue)
newList
Outcome
[1] "A" "A" "3"
I do not want the "3" in the above list, but I want "C" instead. Does anybody know how I can acccess the "C" in "memberLargestValue" and get it into the list?
Solution:
Change to "character" type:
memberLargestValue = as.character(df[which.max(df$value),]$name)
edit: I rewrote the whole post including an example that is possible to replicate directly, and also containing the solution provided by Paweł Chabros. Thank you Paweł Chabros for providing a very neat answer!
In the following picture I struggle reversing the order of the box-plots, wanting to change it to go from October to December when looking left to right:
Click here to display plot
The dataframe is created by
library(dplyr)
library(ggplot2)
library(forcats)
name <- c('A','A','A', 'A','A','A', 'A','A','A',
'B','B','B', 'B','B','B', 'B','B','B',
'C','C','C', 'C','C','C', 'C','C','C')
month = c("oct 2018", "oct 2018", "oct 2018","nov 2018", "nov 2018", "nov 2018","dec 2018", "dec 2018", "dec 2018",
"oct 2018", "oct 2018", "oct 2018","nov 2018", "nov 2018", "nov 2018","dec 2018", "dec 2018", "dec 2018" ,
"oct 2018", "oct 2018", "oct 2018","nov 2018", "nov 2018", "nov 2018","dec 2018", "dec 2018", "dec 2018" )
value <- seq(1:length(month))
df = data.frame(name, month, value)
df
The data frame looks like this
name month value
A oct 2018 1
A oct 2018 2
A oct 2018 3
A nov 2018 4
A nov 2018 5
A nov 2018 6
A dec 2018 7
A dec 2018 8
A dec 2018 9
B oct 2018 10
B oct 2018 11
B oct 2018 12
B nov 2018 13
B nov 2018 14
B nov 2018 15
B dec 2018 16
B dec 2018 17
B dec 2018 18
C oct 2018 19
C oct 2018 20
C oct 2018 21
C nov 2018 22
C nov 2018 23
C nov 2018 24
C dec 2018 25
C dec 2018 26
C dec 2018 27
The plot in the figure above is created by
wantedMonths = c("oct 2018", "nov 2018", "dec 2018")
wantedNames = c("A", "B")
df2= df[df$name %in% wantedNames, ]
ggplot(df2[df2$month %in% wantedMonths , ]) + geom_boxplot(aes(as.factor(name), value, fill=month))#fct_rev(month)
The command that creates the correct plot, which was provided by Paweł Chabros, is
ggplot(df2[df2$month %in% wantedMonths , ]) + geom_boxplot(aes(as.factor(name), value, fill=fct_rev(month)))
ggplot uses the order of the factor for this purpose. You can set month as ordered factor either inside ggplot call or change it before, in the data. In that case just add the following line before ggplot call:
df[['month']] = ordered(df[['month']], levels = c('oct 2018', 'nov 2018', 'dec 2018'))
If your problem is the ordering of the bar you can set them manually by scale_colour_manual function.
Just add the this while plotting with ggplot.
scale_colour_manual(values = c("red","green","blue"))
The answer, which is also included in the edited question, is to use fct_rev:
ggplot(df2[df2$month %in% wantedMonths , ]) + geom_boxplot(aes(as.factor(name), value, fill=fct_rev(month)))
This is the string I want to split:
b[1]
[1] "County January 2016 February 2016 March 2016 April 2016 May 2016 June 2016 July 2016 August 2016 September 2016 October 2016 November 2016 December 2016\r"
From this post split string with regex I gather there is no ready-made function to do so, I just want to confirm than.
Here is my code
split.pos <- gregexpr("County|([aA-zZ]{1,} [0-9]{4,})", b[1], perl = FALSE)
split.length <- attr(split.pos[[1]], "match.length")
split.start <- split.pos[[1]][1:length(split.pos[[1]])]
substring(b[1], split.start, split.start+split.length)
[1] "County " "January 2016 " "February 2016 " "March 2016 "
[5] "April 2016 " "May 2016 " "June 2016 " "July 2016 "
[9] "August 2016 " "September 2016 " "October 2016 " "November 2016 "
[13] "December 2016\r
Is there a better way of doing this? Thanks
We can use strsplit with regex lookaround
strsplit(b, "(?<=[0-9])\\s+|\\s+(?=[A-Z])", perl = TRUE)[[1]]
#[1] "County" "January 2016" "February 2016" "March 2016" "April 2016" "May 2016" "June 2016" "July 2016" "August 2016"
#[10] "September 2016" "October 2016" "November 2016" "December 2016"
data
b <- "County January 2016 February 2016 March 2016 April 2016 May 2016 June 2016 July 2016 August 2016 September 2016 October 2016 November 2016 December 2016\r"