how to set value for whole row through pipe function - r

I am trying to set the default value for the first row in of the data frame to 1 through using dplyr 's pipe function.
As its one of the requirements to dynamically compound the values in the data with the date function. I will appreciate if anyone can help me to understand how I can set the default value for all the 1st rows in below mentioned code. I already created a placeholder where I m expected default value .
df2 <- df %>% mutate(MonthDate = as.Date(df$StartMonth,"%m/%d/%Y")) %>%
filter(MonthDate > as.Date("2005-09-30"))%>%
# Place holder where I want to set first row of the column to default value of 1#
mutate(SAABenchmark_VAMI = cumprod(exp(SAABenchmarkstdev)),
SAABetaOnly_VAMI = cumprod(exp(SAABetaOnly_stdev)),
HFbenchmark_VAMI = cumprod(exp(HFbenchmarkReturns)),
ZAAMHF_VAMI = cumprod(exp(ZAAMHFReturns)))%>%
mutate(SAABenchmark_logVAMI =log(SAABenchmark_VAMI),
SAABetaOnly_logVAMI=log (SAABetaOnly_VAMI),
Hfbenchmark_logVAMI=log(HFbenchmark_VAMI),
ZAAMHF_logVAMI=log(ZAAMHF_VAMI))%>%
select(MonthDate,SAABenchmark_VAMI:ZAAMHF_logVAMI)%>%
melt(., id.vars = "MonthDate")
Kindly find my dataframe as well :
structure(list(StartMonth = structure(c(2L, 9L, 11L, 13L, 15L,
17L, 19L, 21L, 23L, 4L, 6L, 8L, 3L, 10L, 12L, 14L, 16L, 18L,
20L, 22L, 24L, 5L, 7L, 1L), .Label = c("", "1/31/2017", "1/31/2018",
"10/31/2017", "10/31/2018", "11/30/2017", "11/30/2018", "12/31/2017",
"2/28/2017", "2/28/2018", "3/31/2017", "3/31/2018", "4/30/2017",
"4/30/2018", "5/31/2017", "5/31/2018", "6/30/2017", "6/30/2018",
"7/31/2017", "7/31/2018", "8/31/2017", "8/31/2018", "9/30/2017",
"9/30/2018"), class = "factor"), SAABenchmarkstdev = c(47L, 48L,
49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L,
62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, NA), SAABetaOnly_stdev = c(26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L,
40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, NA), HFbenchmarkReturns = c(34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, NA), ZAAMHFReturns = c(47L,
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L,
61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, NA)), class = "data.frame", row.names = c(NA,
-24L))

You could do something with mutate_at().
Here I remove the columns you don't want to add 1's as the first row in vars(). Then to set the first row to 1 in the remaining columns I concatenate 1 with the vector of values minus the first value.
df %>%
mutate(MonthDate = as.Date(df$StartMonth,"%m/%d/%Y")) %>%
filter(MonthDate > as.Date("2005-09-30")) %>%
mutate_at(vars(-MonthDate, -StartMonth), list(~c(1, .[-1])))
StartMonth SAABenchmarkstdev SAABetaOnly_stdev HFbenchmarkReturns ZAAMHFReturns MonthDate
1 1/31/2017 1 1 1 1 2017-01-31
2 2/28/2017 48 27 35 48 2017-02-28
3 3/31/2017 49 28 36 49 2017-03-31
...

Related

How to convert list of dates to POSIXlt objects?

I want to run the bfastts function (https://www.rdocumentation.org/packages/bfast/versions/1.5.7/topics/bfastts) on a series of dates formatted as dd-mm-yyyy and a series of values to convert them into a time series. For this function the dates need to be of "POSIXlt" type. However, when running the code
dv<-as.POSIXct.POSIXlt(dates, tz="")
I am getting the error
Error in as.POSIXct.POSIXlt(dates, tz = "") : invalid 'x' argument
When running
dv<-strftime(as.POSIXct.POSIXlt(dates, tz="")
The returned list seems to be empty. When inputting my date list without conversion into the bfastts I'm getting the error
Error in as.POSIXlt.default(dates) :
do not know how to convert 'dates' to class “POSIXlt”
I am not used to coding in R as I usually work in python. I've tried googling all three errors but I can't find a solution. Could any one provide me some pointers?
Edit:
dput(dates) gives me:
list(V1 = structure(c(19L, 57L, 31L, 59L, 33L, 34L, 4L, 7L, 40L,
12L, 50L, 56L, 3L, 37L, 6L, 39L, 46L, 17L, 43L, 55L, 30L, 2L,
36L, 38L, 11L, 21L, 49L, 24L, 27L, 10L, 45L, 14L, 1L, 18L, 47L,
54L, 29L, 32L, 8L, 42L, 9L, 16L, 44L, 48L, 23L, 51L, 52L, 26L,
35L, 5L, 15L, 20L, 22L, 25L, 53L, 28L, 58L, 41L, 13L), .Label = c("1-8-
2016", "11-5-2015", "11-7-2014", "12-10-2013", "12-2-2018", "12-8-2014",
"13-11-2013", "13-3-2017", "14-4-2017", "14-6-2016", "14-7-2015",
"15-12-2013", "15-2-2019", "16-7-2016", "17-4-2018", "17-6-2017",
"18-12-2014", "18-9-2016", "19-4-2013", "19-5-2018", "2-10-2015",
"20-6-2018", "20-8-2017", "21-12-2015", "22-7-2018", "23-10-2017",
"23-2-2016", "23-8-2018", "24-1-2017", "24-3-2015", "24-7-2013",
"25-2-2017", "25-8-2013", "26-9-2013", "27-1-2018", "27-5-2015",
"27-7-2014", "28-6-2015", "28-8-2014", "29-11-2013", "29-12-2018",
"29-3-2017", "3-1-2015", "3-7-2017", "30-6-2016", "31-10-2014",
"4-10-2016", "4-8-2017", "5-12-2015", "5-3-2014", "5-9-2017",
"7-10-2017", "7-8-2018", "8-1-2017", "8-3-2015", "8-5-2014",
"8-7-2013", "8-9-2018", "9-8-2013"), class = "factor"))
The problem is that dates is a list, but you actually want to access the first entry (V1) of it. Further you have to specify that the dates you are providing are in the format dd-mm-yyyy. This you can do with format = "%d-%m-%Y". Thus the following works:
as.POSIXlt(dates$V1, format = "%d-%m-%Y", tz="")
# [1] "2013-04-19 CEST" "2013-07-08 CEST" "2013-07-24 CEST" "2013-08-09 CEST"
# ...
Data
dates <- list(V1 = structure(c(19L, 57L, 31L, 59L, 33L, 34L, 4L, 7L, 40L,
12L, 50L, 56L, 3L, 37L, 6L, 39L, 46L, 17L, 43L, 55L, 30L, 2L,
36L, 38L, 11L, 21L, 49L, 24L, 27L, 10L, 45L, 14L, 1L, 18L, 47L,
54L, 29L, 32L, 8L, 42L, 9L, 16L, 44L, 48L, 23L, 51L, 52L, 26L,
35L, 5L, 15L, 20L, 22L, 25L, 53L, 28L, 58L, 41L, 13L),
.Label = c("1-8-2016", "11-5-2015", "11-7-2014", "12-10-2013", "12-2-2018", "12-8-2014",
"13-11-2013", "13-3-2017", "14-4-2017", "14-6-2016", "14-7-2015",
"15-12-2013", "15-2-2019", "16-7-2016", "17-4-2018", "17-6-2017",
"18-12-2014", "18-9-2016", "19-4-2013", "19-5-2018", "2-10-2015",
"20-6-2018", "20-8-2017", "21-12-2015", "22-7-2018", "23-10-2017",
"23-2-2016", "23-8-2018", "24-1-2017", "24-3-2015", "24-7-2013",
"25-2-2017", "25-8-2013", "26-9-2013", "27-1-2018", "27-5-2015",
"27-7-2014", "28-6-2015", "28-8-2014", "29-11-2013", "29-12-2018",
"29-3-2017", "3-1-2015", "3-7-2017", "30-6-2016", "31-10-2014",
"4-10-2016", "4-8-2017", "5-12-2015", "5-3-2014", "5-9-2017",
"7-10-2017", "7-8-2018", "8-1-2017", "8-3-2015", "8-5-2014",
"8-7-2013", "8-9-2018", "9-8-2013"), class = "factor"))

R check if there is a letter and then multiply the value by -1

I have a dataframe (databycitydiff2) column as follows:
x
10N
20N
35S
25S
What I want to do is check if the letter at the end is N or S. If it is S, I want to remove the S and multiply the number by -1. If it is N, then I just want to remove the N.
I tried the following but it adds "-" to all values:
databycitydiff2$x<-gsub( "N", "", databycitydiff2$x)
databycitydiff2$x<-sub("^","-", gsub( "S", "", databycitydiff2$x))
What I get is:
x
-10
-20
-35
-25
What I want is:
x
10
20
-35
-25
Any suggestions? thank you!
Dput of the dataframe column (before i changed it to as.character):
structure(c(57L, 47L, 62L, 45L, 45L, 57L, 62L, 55L, 29L, 55L,
60L, 54L, 70L, 70L, 62L, 13L, 55L, 37L, 33L, 29L, 70L, 23L, 72L,
11L, 72L, 55L, 19L, 51L, 62L, 29L, 37L, 72L, 36L, 7L, 17L, 71L,
9L, 41L, 29L, 21L, 55L, 37L, 25L, 19L, 21L, 13L, 29L, 31L, 49L,
21L, 31L, 25L, 35L, 37L, 41L, 17L, 45L, 39L, 45L, 49L, 70L, 17L,
6L, 7L, 37L, 72L, 41L, 26L, 35L, 38L, 45L, 45L, 45L, 37L, 41L,
37L, 37L, 27L, 23L, 45L, 47L, 37L, 58L, 61L, 55L, 53L, 27L, 41L,
35L, 55L, 35L, 29L, 13L, 2L, 7L, 44L, 5L, 22L, 58L, 54L, 37L,
19L, 31L, 27L, 58L, 12L, 72L, 33L, 21L, 2L, 9L, 21L, 65L, 49L,
51L, 45L, 58L, 9L, 53L, 22L, 45L, 35L, 33L, 41L, 47L, 31L, 37L,
45L, 25L, 37L, 39L, 14L, 39L, 9L, 22L, 18L, 57L, 55L, 37L, 49L,
58L, 25L, 7L, 22L, 57L, 23L, 20L, 4L, 51L, 71L, 35L, 4L, 20L,
20L, 22L, 24L, 39L, 12L, 14L, 71L, 67L, 41L, 51L, 43L, 58L, 44L,
41L, 37L, 61L, 49L, 37L, 21L, 33L, 39L, 31L, 37L, 31L, 9L, 57L,
71L, 51L, 24L, 21L, 25L, 39L, 22L, 35L, 37L, 53L, 38L, 53L, 17L,
45L, 29L, 7L, 70L, 55L, 8L, 55L, 25L, 5L, 70L, 71L, 37L, 72L,
51L, 3L, 9L, 41L, 45L, 47L, 13L, 55L, 25L, 33L, 37L, 11L, 11L,
27L, 21L, 29L, 29L, 42L, 5L, 27L, 53L, 51L, 21L, 37L, 37L, 45L,
58L, 61L, 19L, 2L, 9L, 25L, 21L, 53L, 41L, 23L, 5L, 5L, 55L,
23L, 55L, 39L, 15L, 15L, 44L, 71L, 37L, 5L, 21L, 7L, 43L, 49L,
71L, 44L, 21L, 21L, 23L, 73L, 41L, 35L, 53L, 21L, 21L, 21L, 20L,
3L, 21L, 27L, 45L, 72L, 19L, 21L, 29L, 60L, 3L, 20L, 70L, 3L,
6L, 18L, 49L, 13L, 22L, 3L, 53L, 58L, 21L, 25L, 31L, 15L, 31L,
7L, 53L, 39L, 29L, 44L, 3L, 20L, 17L, 35L, 29L, 25L, 23L, 53L,
29L, 19L, 45L, 18L, 35L, 56L, 2L, 43L, 61L, 22L, 9L, 57L, 49L,
9L, 53L, 55L, 18L, 33L, 33L, 33L, 44L, 6L, 29L, 29L, 26L, 45L,
33L, 3L, 63L, 51L, 64L, 57L, 39L, 58L, 53L, 43L, 53L, 26L, 18L,
25L, 7L, 21L, 23L, 23L, 27L, 19L, 21L, 19L, 19L, 23L, 11L, 25L,
17L, 27L, 21L, 17L, 21L, 19L, 3L, 60L, 13L, 72L, 13L, 58L, 57L,
23L, 11L, 27L, 54L, 19L, 47L, 7L, 43L, 15L, 39L, 25L, 25L, 20L,
33L, 33L, 5L, 34L, 5L, 1L, 58L, 60L, 60L, 57L, 10L, 73L, 30L,
26L, 15L, 3L, 58L, 71L, 43L, 70L, 39L, 21L, 26L, 49L, 60L, 56L,
13L, 57L, 1L, 49L, 35L, 37L, 47L, 19L, 53L, 30L, 58L, 22L, 70L,
54L, 57L, 33L, 60L, 45L, 22L, 13L, 51L, 26L, 10L, 51L, 53L, 61L,
44L, 71L, 58L, 60L, 60L, 51L, 54L, 45L, 57L, 7L, 28L, 58L, 24L,
54L, 25L, 57L, 58L, 57L, 60L, 70L, 49L, 53L, 27L, 43L, 47L, 43L,
43L, 29L, 44L, 37L, 16L, 55L, 27L, 9L, 18L, 5L, 25L, 35L, 29L,
47L, 47L, 19L, 41L, 29L, 1L, 55L, 72L, 51L, 60L, 57L, 72L, 13L,
9L, 3L, 22L, 3L, 30L, 18L, 37L, 54L, 72L, 41L, 3L, 9L, 12L, 29L,
71L, 55L, 72L, 22L, 7L, 21L, 57L, 43L, 19L, 5L, 6L, 19L, 71L,
47L, 17L, 71L, 22L, 18L, 20L, 3L, 38L, 19L, 39L, 30L, 3L, 70L,
17L, 55L, 25L, 34L, 24L, 3L, 3L, 22L, 58L, 18L, 13L, 13L, 13L,
33L, 3L, 43L, 73L, 37L, 33L, 24L, 2L, 28L, 20L, 39L, 3L, 44L,
9L, 56L, 30L, 3L, 47L, 17L, 33L, 17L, 21L, 21L, 27L, 31L, 33L,
21L, 17L, 49L, 27L, 49L, 31L, 31L, 45L, 21L, 26L, 3L, 57L, 33L,
35L, 21L, 35L, 72L, 61L, 58L, 58L, 61L, 57L, 45L, 31L, 21L, 25L,
63L, 54L, 49L, 58L, 54L, 37L, 13L, 25L, 23L, 19L, 13L, 47L, 71L,
47L, 37L, 27L, 7L, 38L, 13L, 17L, 73L, 14L, 5L, 8L, 6L, 31L,
29L, 53L, 58L, 7L, 23L, 14L, 31L, 37L, 9L, 7L, 29L, 57L, 6L,
50L, 33L, 19L, 23L, 23L, 27L, 33L, 71L, 71L, 71L, 71L, 56L, 56L,
71L, 19L, 19L, 71L, 56L, 56L, 56L, 56L, 71L, 71L, 41L, 71L, 71L,
56L, 71L, 71L, 56L, 56L, 72L, 72L, 19L, 37L, 27L, 51L, 45L, 53L,
17L, 13L, 12L, 45L, 18L, 58L, 17L, 57L, 70L, 41L, 35L, 41L, 52L,
72L, 38L, 39L, 32L, 18L, 49L, 37L, 18L, 61L, 28L, 30L, 25L, 32L,
58L, 5L, 33L, 18L, 27L, 28L, 33L, 70L, 22L, 70L, 58L, 58L, 57L,
49L, 57L, 28L, 17L, 13L, 22L, 9L, 16L, 13L, 10L, 23L, 3L, 36L,
24L, 56L, 8L, 57L, 5L, 9L, 57L, 29L, 57L, 55L, 25L, 11L, 37L,
35L, 9L, 25L, 41L, 21L, 33L, 70L, 33L, 5L, 21L, 41L, 21L, 71L,
25L, 25L, 57L, 33L, 7L, 45L, 45L, 61L, 70L, 7L, 41L, 31L, 41L,
72L, 53L, 29L, 23L, 49L, 31L, 27L, 27L, 13L, 39L, 73L, 33L, 41L,
25L, 71L, 29L, 31L, 47L, 60L, 45L, 3L, 47L, 21L, 31L, 33L, 39L,
21L, 21L, 25L, 9L, 25L, 17L, 22L, 27L, 51L, 70L, 53L, 25L, 60L,
23L, 3L, 41L, 9L, 3L, 18L, 55L, 3L, 33L, 56L, 23L, 21L, 17L,
45L, 33L, 39L, 58L, 58L, 39L, 55L, 43L, 20L, 35L, 57L, 49L, 23L,
60L, 58L, 57L, 21L, 33L, 72L, 15L, 62L, 52L, 47L, 22L, 23L, 30L,
19L, 21L, 37L, 45L, 41L, 62L, 34L, 35L, 57L, 47L, 37L, 17L, 43L,
70L, 61L, 47L, 45L, 60L, 58L, 72L, 70L, 58L, 29L, 3L, 31L, 35L,
31L, 9L, 72L, 45L, 60L, 1L, 61L, 51L, 45L, 11L, 22L, 58L, 19L,
57L, 58L, 31L, 70L, 55L, 57L, 54L, 5L, 41L, 41L, 33L, 9L, 45L,
33L, 41L, 37L, 18L, 64L, 57L, 27L, 25L, 49L, 39L, 57L, 29L, 54L,
39L, 31L, 25L, 27L, 21L, 27L, 25L, 35L, 6L, 45L, 21L, 24L, 49L,
22L, 35L, 20L, 25L, 31L, 47L, 49L, 15L, 28L, 70L, 51L, 45L, 35L,
49L, 26L, 45L, 25L, 45L, 33L, 44L, 21L, 24L, 18L, 22L, 22L, 57L,
72L, 53L, 39L, 21L, 37L, 37L, 45L, 35L, 35L, 37L, 35L, 37L, 37L,
35L, 29L, 33L, 37L, 5L, 45L, 47L, 33L, 51L, 25L, 63L, 58L, 23L,
24L, 9L, 55L, 29L, 51L, 71L, 21L, 21L, 29L, 25L, 9L, 25L, 37L,
19L, 73L, 33L, 33L, 33L, 72L, 71L, 25L, 7L, 23L, 39L, 21L, 33L,
70L, 70L, 60L, 60L, 45L, 40L, 21L, 58L, 43L, 25L, 51L, 49L, 57L,
36L, 70L, 57L, 45L, 27L, 23L, 49L, 33L, 57L, 70L, 43L, 45L, 29L,
61L, 63L, 35L, 57L, 58L, 21L, 12L, 31L, 28L, 16L, 3L, 58L, 39L,
25L, 19L, 5L, 49L, 25L, 37L, 1L, 58L, 14L, 39L, 33L, 47L, 30L,
53L, 49L, 37L, 51L, 71L, 60L, 30L, 3L, 17L, 25L, 72L, 31L, 33L,
21L, 17L, 18L, 24L, 22L, 3L, 22L, 22L, 9L, 9L, 3L, 16L, 27L,
5L, 11L, 49L, 23L, 25L, 33L, 33L, 11L, 15L, 23L, 9L, 11L, 27L,
5L, 25L, 25L, 18L, 53L, 45L, 19L, 17L, 58L, 58L, 35L, 21L, 45L,
37L, 37L, 5L, 31L, 58L, 9L, 19L, 41L, 45L, 33L, 23L, 54L, 53L,
21L, 23L, 45L, 19L, 29L, 49L, 21L, 57L, 35L, 35L, 60L, 47L, 47L,
58L, 37L, 35L, 37L, 41L, 29L, 19L, 58L, 29L, 33L, 21L, 27L, 14L,
51L, 25L, 72L, 29L, 45L, 7L, 70L, 27L, 39L, 21L, 37L, 31L, 53L,
54L, 57L, 54L, 64L, 37L, 51L, 39L, 25L, 11L, 35L, 29L, 58L, 35L,
25L, 13L, 41L, 35L, 58L, 35L, 7L, 37L, 35L, 37L, 45L, 35L, 29L,
37L, 3L, 48L, 35L, 45L, 17L, 25L, 19L, 51L, 29L, 54L, 22L, 21L,
31L, 9L, 5L, 37L, 29L, 60L, 23L, 23L, 23L, 4L, 47L, 35L, 27L,
33L, 33L, 8L, 6L, 41L, 21L, 27L, 29L, 9L, 60L, 11L, 39L, 21L,
17L, 21L, 51L, 53L, 21L, 33L, 35L, 31L, 23L, 53L, 70L, 43L, 35L,
1L, 7L, 18L, 8L, 11L, 35L, 37L, 35L, 37L, 70L, 72L, 13L, 37L,
70L, 70L, 70L, 70L, 70L, 70L, 35L, 70L, 55L, 70L, 33L, 70L, 44L,
72L, 10L, 72L, 70L, 3L, 72L, 35L, 56L, 23L, 35L, 22L, 41L, 41L,
21L, 56L, 41L, 35L, 54L, 23L, 70L, 53L, 18L, 43L, 58L, 18L, 44L,
35L, 25L, 17L, 33L, 37L, 71L, 58L, 37L, 33L, 33L, 70L, 37L, 37L,
70L, 41L, 15L, 37L, 33L, 31L, 39L, 45L, 22L, 10L, 55L, 22L, 26L,
35L, 22L, 22L, 22L, 22L, 21L, 22L, 62L, 39L, 35L, 37L, 70L, 17L,
62L, 39L, 45L, 35L, 39L, 20L, 21L, 73L, 22L, 33L, 29L, 27L, 72L,
29L, 27L, 25L, 56L, 35L, 31L, 72L, 17L, 25L, 23L, 1L, 2L, 33L,
19L, 21L, 21L, 22L, 26L, 33L, 31L, 25L, 73L, 8L, 37L, 45L, 31L,
19L, 31L, 23L, 33L, 27L, 4L, 53L, 25L, 21L, 31L, 31L, 49L, 27L,
37L, 35L, 31L, 41L, 21L, 5L, 19L, 21L, 33L, 49L, 72L, 70L, 37L,
39L, 23L, 29L, 29L, 29L, 45L, 45L, 45L, 27L, 27L, 49L, 29L, 45L,
49L, 13L, 51L, 71L, 25L, 39L, 26L, 1L, 26L, 45L, 55L, 71L, 3L,
73L, 31L, 71L, 43L, 20L, 24L, 10L, 19L, 22L, 41L, 3L, 35L, 8L,
14L, 3L, 39L, 35L, 25L, 29L, 21L, 37L, 11L, 35L, 56L, 61L, 58L,
39L, 70L, 21L, 60L, 17L, 37L, 35L, 31L, 21L, 61L, 31L, 1L, 57L,
56L, 7L, 21L, 3L, 70L, 3L, 5L, 25L, 41L, 61L, 21L, 7L, 11L, 23L,
37L, 39L, 51L, 71L, 56L, 13L, 35L, 54L, 29L, 35L, 41L, 35L, 29L,
37L, 9L, 57L, 33L, 35L, 37L, 31L, 27L, 23L, 57L, 7L, 73L, 61L,
37L, 37L, 35L, 37L, 41L, 61L, 71L, 53L, 71L, 71L, 23L, 54L, 71L,
61L, 44L, 35L, 51L, 29L, 35L, 54L, 27L, 11L, 19L, 29L, 31L, 27L,
19L, 21L, 9L, 53L, 61L, 7L, 33L, 11L, 33L, 29L, 39L, 45L, 21L,
27L, 33L, 27L, 60L, 57L, 57L, 16L, 44L, 56L, 31L, 28L, 3L, 16L,
13L, 60L, 44L, 37L, 63L, 54L, 37L, 1L, 15L, 35L, 72L, 35L, 49L,
2L, 2L, 35L, 49L, 49L, 6L, 61L, 15L, 71L, 26L, 15L, 37L, 35L,
57L, 3L, 37L, 37L, 33L, 72L, 37L, 7L, 11L, 72L, 61L, 64L, 35L,
37L, 57L, 37L, 39L, 19L, 72L, 39L, 45L, 55L, 37L, 54L, 62L, 60L,
29L, 23L, 55L, 55L, 70L, 5L, 62L, 70L, 49L, 51L, 61L, 57L, 54L,
56L, 21L, 28L, 26L, 53L, 15L, 55L, 43L, 1L, 33L, 6L, 55L, 21L,
37L, 47L, 70L, 43L, 3L, 23L, 4L, 35L, 35L, 61L, 58L, 33L, 35L,
7L, 35L, 49L, 14L, 58L, 9L, 23L, 36L, 30L, 13L, 30L, 13L, 41L,
60L, 17L, 29L, 72L, 72L, 28L, 55L, 55L, 31L, 37L, 37L, 41L, 27L,
25L, 27L, 24L, 13L, 35L, 41L, 47L, 37L, 13L, 33L, 3L, 27L, 27L,
27L, 19L, 37L, 29L, 21L, 45L, 13L, 51L, 71L, 54L, 53L, 5L, 60L,
45L, 7L, 49L, 57L, 58L, 58L, 57L, 25L, 71L, 35L, 27L, 60L, 29L,
71L, 57L, 39L, 57L, 19L, 23L, 37L, 45L, 1L, 21L, 8L, 4L, 57L,
8L, 6L, 20L, 51L, 27L, 45L, 37L, 27L, 18L, 37L, 37L, 41L, 37L,
54L, 7L, 58L, 15L, 41L, 35L, 21L, 23L, 60L, 14L, 51L, 45L, 2L,
6L, 47L, 44L, 47L, 22L, 33L, 3L, 51L, 53L, 47L, 23L, 27L, 35L,
25L, 17L, 3L, 27L, 9L, 39L, 55L, 47L, 46L, 31L, 39L, 73L, 8L,
10L, 33L, 57L, 6L, 7L, 23L, 31L, 54L, 73L, 33L, 35L, 27L, 24L,
10L, 58L, 25L, 29L, 12L, 57L, 51L, 61L, 37L, 37L, 19L, 57L, 57L,
58L, 45L, 31L, 57L, 23L, 9L, 20L, 1L, 4L, 44L, 31L, 37L, 9L,
7L, 21L, 47L, 71L, 7L, 45L, 3L, 22L, 72L, 58L, 71L, 60L, 37L,
10L, 11L, 21L, 5L, 5L, 57L, 5L, 71L, 56L, 9L, 47L, 53L, 70L,
43L, 72L, 3L, 71L, 39L, 17L, 29L, 61L, 9L, 70L, 1L, 5L, 25L,
16L, 37L, 47L, 19L, 3L, 33L, 23L, 5L, 7L, 9L, 41L, 55L, 54L,
2L, 13L, 26L, 19L, 26L, 40L, 56L, 3L, 44L, 3L, 7L, 39L, 20L,
41L, 70L, 37L, 35L, 3L, 22L, 37L, 3L, 31L, 47L, 44L, 29L, 5L,
30L, 37L, 31L, 56L, 25L, 21L, 45L, 73L, 27L, 26L, 35L, 37L, 37L,
35L, 3L, 25L, 22L, 11L, 22L, 49L, 19L, 21L, 58L, 1L, 73L, 55L,
7L, 56L, 19L, 43L, 55L, 17L, 19L, 7L, 15L, 40L, 35L, 34L, 41L,
34L, 5L, 37L, 33L, 33L, 39L, 29L, 13L, 56L, 54L, 33L, 17L, 9L,
60L, 25L, 37L, 31L, 61L, 26L, 60L, 26L, 16L, 51L, 47L, 13L, 35L,
37L, 72L, 51L, 60L, 25L, 43L, 23L, 23L, 37L, 35L, 53L, 33L, 37L,
9L, 31L, 31L, 70L, 49L, 39L, 57L, 31L, 15L, 22L, 71L, 3L, 44L,
25L, 70L, 9L, 72L, 25L, 12L, 36L, 33L, 49L, 51L, 19L, 51L, 9L,
27L, 70L, 17L, 25L, 35L, 35L, 9L, 71L, 21L, 61L, 44L, 56L, 47L,
25L, 72L, 3L, 49L, 6L, 53L, 29L, 23L, 53L, 39L, 31L, 68L, 61L,
21L, 37L, 2L, 14L, 29L, 29L, 25L, 16L, 71L, 3L, 19L, 61L, 49L,
10L, 21L, 35L, 7L, 39L, 37L, 21L, 72L, 35L, 19L, 21L, 2L, 11L,
47L, 7L, 9L, 72L, 2L, 47L, 11L, 17L, 45L, 10L, 57L, 53L, 27L,
31L, 54L, 13L, 19L, 9L, 27L, 31L, 21L, 45L, 53L, 31L, 21L, 33L,
45L, 45L, 37L, 21L, 37L, 21L, 37L, 23L, 56L, 17L, 21L, 41L, 17L,
23L, 25L, 39L, 49L, 72L, 8L, 61L, 65L, 70L, 54L, 29L, 15L, 24L,
33L, 42L, 57L, 49L, 27L, 45L, 29L, 45L, 45L, 28L, 61L, 58L, 39L,
35L, 37L, 5L, 17L, 70L, 7L, 5L, 7L, 49L, 17L, 26L, 39L, 35L,
37L, 58L, 22L, 29L, 47L, 35L, 35L, 22L, 13L, 61L, 64L, 62L, 62L,
55L, 55L, 55L, 33L, 37L, 31L, 61L, 7L, 37L, 69L, 35L, 37L, 38L,
58L, 35L, 58L, 4L, 60L, 13L, 20L, 22L, 51L, 51L, 30L, 61L, 53L,
60L, 60L, 49L, 61L, 57L, 70L, 7L, 27L, 47L, 35L, 54L, 53L, 70L,
39L, 11L, 57L, 47L, 61L, 55L, 27L, 55L, 33L, 3L, 37L, 61L, 51L,
61L, 72L, 57L, 35L, 72L, 31L, 35L, 37L, 43L, 60L, 41L, 58L, 60L,
71L, 54L, 9L, 60L, 45L, 37L, 61L, 33L, 70L, 9L, 55L, 35L, 43L,
57L, 58L, 53L, 25L, 33L, 45L, 61L, 13L, 26L, 53L, 27L, 55L, 60L,
57L, 39L, 14L, 22L, 49L, 70L, 64L, 39L, 58L, 46L, 57L, 37L, 47L,
51L, 70L, 5L, 67L, 41L, 47L, 55L, 70L, 58L, 35L, 37L, 70L, 39L,
12L, 73L, 51L, 13L, 36L, 21L, 71L, 17L, 71L, 2L, 58L, 51L, 70L,
7L, 19L, 29L, 3L, 23L, 16L, 39L, 28L, 25L, 7L, 41L, 17L, 4L,
35L, 43L, 3L, 2L, 27L, 56L, 47L, 56L, 72L, 23L, 61L, 71L, 21L,
29L, 13L, 25L, 37L, 72L, 55L, 32L, 24L, 17L, 54L, 49L, 16L, 56L,
41L, 56L, 35L, 56L, 71L, 1L, 28L, 1L, 71L, 21L, 23L, 45L, 33L,
11L, 71L, 29L, 23L, 14L, 71L, 58L, 54L, 2L, 71L, 41L, 32L, 71L,
8L, 25L, 60L, 45L, 43L, 55L, 39L, 63L, 47L, 32L, 47L, 62L, 47L,
35L, 15L, 9L, 3L, 58L, 22L, 73L, 61L, 60L, 65L, 54L, 31L, 22L,
3L, 41L, 11L, 5L, 33L, 7L, 53L, 27L, 30L, 22L, 27L, 13L, 19L,
22L, 3L, 23L, 35L, 37L, 21L, 21L, 27L, 58L, 22L, 49L, 45L, 56L,
7L, 33L, 49L, 58L, 51L, 47L, 71L, 57L, 57L, 22L, 20L, 47L, 61L,
27L, 54L, 35L, 13L, 5L, 3L, 24L, 2L, 57L, 19L, 13L, 5L, 36L,
2L, 43L, 18L, 73L, 31L, 25L, 51L, 13L, 45L, 30L, 2L, 57L, 28L,
26L, 22L, 18L, 58L, 20L, 58L, 44L, 57L, 22L, 49L, 20L, 60L, 24L,
56L, 47L, 47L, 71L, 9L, 60L, 47L, 41L, 62L, 73L, 3L, 13L, 39L,
3L, 46L, 17L, 3L, 17L, 41L, 13L, 10L, 59L, 5L, 29L, 17L, 25L,
21L, 71L, 71L, 71L, 71L, 37L, 49L, 13L, 35L, 29L, 31L, 37L, 37L,
25L, 51L, 29L, 37L, 21L, 41L, 19L, 53L, 51L, 35L, 60L, 23L, 39L,
53L, 32L, 22L, 14L, 41L, 17L, 29L, 9L, 2L, 7L, 34L, 2L, 23L,
37L, 7L, 35L, 3L, 57L, 25L, 29L, 11L, 23L, 19L, 19L, 11L, 72L,
19L, 21L, 23L, 37L, 33L, 51L, 27L, 71L, 36L, 21L, 35L, 26L, 53L,
56L, 11L, 25L, 21L, 15L, 39L, 54L, 21L, 19L, 49L, 33L, 43L, 5L,
57L, 73L, 58L, 39L, 57L, 54L, 57L, 56L, 53L, 41L, 20L, 28L, 54L,
23L, 27L, 67L, 25L, 35L, 18L, 22L, 20L, 28L, 33L, 39L, 62L, 51L,
49L, 4L, 20L, 34L, 22L, 32L, 16L, 21L, 35L, 57L, 23L, 37L, 7L,
47L, 47L, 27L, 47L, 12L, 29L, 34L, 41L, 60L, 53L, 60L, 58L, 54L,
5L, 58L, 57L, 27L, 2L, 49L, 26L, 60L, 63L, 57L, 57L, 22L, 22L,
20L, 22L, 22L, 28L, 18L, 22L, 24L, 30L, 16L, 22L, 22L, 54L, 45L,
18L, 25L, 33L, 37L, 39L, 27L, 35L, 33L, 37L, 21L, 29L, 25L, 31L,
51L, 20L, 60L, 41L, 51L, 27L, 37L, 35L, 41L, 37L, 11L, 17L, 71L,
60L, 49L, 45L, 58L, 37L, 45L, 24L, 23L, 32L, 8L, 53L, 58L, 60L,
2L, 41L, 19L, 27L, 45L, 7L, 29L, 35L, 34L, 46L, 13L, 33L, 9L,
39L, 17L, 3L, 39L, 13L, 17L, 9L, 24L, 34L, 19L, 33L, 34L, 9L,
72L, 25L, 34L, 55L, 7L, 17L, 7L, 13L, 9L, 36L, 7L, 35L, 19L,
23L, 19L, 35L, 55L, 23L, 35L, 25L, 33L, 25L, 39L, 35L, 7L, 27L,
30L, 32L, 18L, 30L, 3L, 71L, 39L, 3L, 49L, 16L, 17L, 17L, 28L,
22L, 13L, 22L, 55L, 30L, 37L, 49L, 60L, 62L, 58L, 31L, 72L, 37L,
23L, 33L, 58L, 45L, 11L, 21L, 23L, 53L, 31L, 35L, 25L, 56L, 37L,
33L, 53L, 55L, 1L, 5L, 71L, 57L, 37L, 19L, 39L, 56L, 56L, 7L,
15L, 62L, 61L, 18L, 41L, 18L, 37L, 67L, 62L, 39L, 70L, 15L, 37L,
27L, 33L, 31L, 37L, 53L, 72L, 31L, 35L, 27L, 21L, 23L, 27L, 29L,
25L, 21L, 23L, 29L, 60L, 31L, 45L, 21L, 51L, 23L, 49L, 27L, 25L,
23L, 21L, 31L, 7L, 35L, 44L, 29L, 29L, 33L, 41L, 13L, 33L, 21L,
51L, 53L, 23L, 41L, 47L, 7L, 33L, 61L, 51L, 15L, 71L, 57L, 39L,
27L, 3L, 23L, 35L, 72L, 1L, 73L, 1L, 7L, 49L, 23L, 29L, 29L,
27L, 41L, 39L, 25L, 47L, 58L, 61L, 43L, 44L, 72L, 49L, 47L, 55L,
25L, 37L, 72L, 7L, 3L, 64L, 57L, 36L, 4L, 27L, 71L, 22L, 2L,
57L, 55L, 45L, 57L, 58L, 26L, 7L, 13L, 49L, 53L, 37L, 47L, 26L,
13L, 35L, 45L, 47L, 57L, 63L, 49L, 47L, 60L, 63L, 60L, 39L, 58L,
54L, 54L, 71L, 53L, 14L, 51L, 29L, 53L, 29L, 35L, 45L, 71L, 71L,
27L, 72L, 56L, 25L, 22L, 71L, 57L, 61L, 55L, 43L, 39L, 37L, 33L,
71L, 71L, 19L, 72L, 21L, 65L, 11L, 31L, 37L, 58L, 22L, 35L, 35L,
58L, 58L, 36L, 65L, 47L, 60L, 53L, 60L, 51L, 37L, 41L, 22L, 56L,
39L, 27L, 37L, 5L, 14L, 53L, 11L, 53L, 70L, 37L, 19L, 23L, 21L,
23L, 43L, 21L, 39L, 33L, 37L, 37L, 35L, 37L, 35L, 55L, 44L, 38L,
38L, 23L, 3L, 31L, 29L, 63L, 37L, 72L, 71L, 7L, 58L, 56L, 27L,
65L, 19L, 40L, 25L, 56L, 56L, 56L, 41L, 37L, 39L, 43L, 23L, 31L,
9L, 51L, 23L, 9L, 45L, 47L, 51L, 33L, 20L, 9L, 57L, 45L, 39L,
63L, 35L, 45L, 71L, 22L, 17L, 40L, 43L, 9L, 45L, 14L, 43L, 71L,
7L, 13L, 45L, 31L, 56L, 55L, 26L, 57L, 33L, 42L, 11L, 35L, 19L,
20L, 56L, 39L, 1L, 47L, 45L, 55L, 12L, 17L, 19L, 13L, 29L, 17L,
3L, 7L, 11L, 58L, 45L, 9L, 72L, 41L, 35L, 3L, 41L, 31L, 53L,
47L, 53L, 33L, 58L, 51L, 56L, 45L, 3L, 72L, 7L, 3L, 5L, 7L, 21L,
17L, 17L, 14L, 37L, 45L, 37L, 35L, 37L, 45L, 22L, 13L, 60L, 47L,
41L, 62L, 17L, 35L, 35L, 49L, 31L, 41L, 25L, 41L, 49L, 35L, 23L,
45L, 58L, 35L, 21L, 47L, 49L, 13L, 53L, 14L, 35L, 35L, 35L, 35L,
45L, 48L, 51L, 54L, 51L, 72L, 33L, 51L, 66L, 73L, 56L, 37L, 37L,
31L, 23L, 31L, 26L, 13L, 60L, 17L, 37L, 43L, 44L, 7L, 21L, 37L,
55L, 39L, 45L, 51L, 64L, 3L, 11L, 49L, 62L, 57L, 62L, 35L, 18L,
14L, 9L, 13L, 11L, 7L, 37L, 61L, 55L, 36L, 35L, 21L, 66L, 53L,
58L, 13L, 54L, 21L, 60L, 9L, 55L, 71L, 25L, 64L, 37L, 9L, 39L,
70L, 17L, 30L, 41L, 49L, 63L, 58L, 44L, 55L, 54L, 10L, 22L, 64L,
19L, 42L, 3L, 45L, 3L, 39L, 34L, 39L, 45L, 51L, 26L, 37L, 23L,
20L, 47L, 21L, 62L, 63L, 5L, 51L, 13L, 19L, 26L, 51L, 26L, 30L,
51L, 54L, 21L, 54L, 13L, 47L, 29L, 11L, 18L, 9L, 9L, 13L, 43L,
51L, 61L, 34L, 13L, 3L, 54L, 54L, 39L, 17L, 28L, 37L, 13L, 37L,
18L, 10L, 73L, 47L, 13L, 47L, 61L, 47L, 53L, 54L, 63L, 20L, 54L,
58L, 28L, 3L, 54L, 31L, 7L, 35L, 37L, 38L, 57L, 58L, 31L, 13L,
17L, 31L, 47L, 55L, 58L, 71L, 41L, 45L, 49L, 58L, 70L, 33L, 37L,
39L, 35L, 28L, 46L, 39L, 27L, 71L, 58L, 35L, 45L, 45L, 41L, 26L,
39L, 35L, 57L, 22L, 47L, 57L, 37L, 26L, 58L, 58L, 58L, 36L, 58L,
47L, 57L, 41L, 27L, 29L, 31L, 51L, 29L, 57L, 39L, 31L, 29L, 21L,
24L, 17L, 23L, 35L, 27L, 33L, 29L, 35L, 21L, 35L, 27L, 17L, 39L,
23L, 37L, 35L, 37L, 37L, 33L, 39L, 45L, 31L, 35L, 35L, 23L, 29L,
37L, 35L, 54L, 70L, 29L, 37L, 33L, 19L, 23L, 33L, 47L, 39L, 35L,
43L, 62L, 33L, 17L, 31L, 62L, 7L, 58L, 17L, 45L, 51L, 27L, 31L,
53L, 37L, 39L, 31L, 45L, 31L, 49L, 27L, 33L, 71L, 35L, 35L, 37L,
25L, 45L, 60L, 39L, 29L, 19L, 35L, 23L, 21L, 31L, 49L, 29L, 58L,
31L, 57L, 21L, 51L, 53L, 37L, 70L, 17L, 37L, 56L, 31L, 35L, 17L,
53L, 45L, 3L, 61L, 61L, 49L, 37L, 45L, 23L, 19L, 39L, 51L, 21L,
25L, 33L, 33L, 53L, 29L, 19L, 23L, 37L, 33L, 37L, 19L, 29L, 61L,
33L, 41L, 27L, 57L, 58L, 29L, 5L, 7L, 61L, 58L, 45L, 37L, 27L,
53L, 33L, 58L), .Label = c("0.80N", "0.80S", "10.45N", "10.45S",
"12.05N", "12.05S", "13.66N", "13.66S", "15.27N", "15.27S", "16.87N",
"16.87S", "18.48N", "18.48S", "2.41N", "2.41S", "20.09N", "20.09S",
"21.70N", "21.70S", "23.31N", "23.31S", "24.92N", "24.92S", "26.52N",
"26.52S", "28.13N", "28.13S", "29.74N", "29.74S", "31.35N", "31.35S",
"32.95N", "32.95S", "34.56N", "34.56S", "36.17N", "36.17S", "37.78N",
"37.78S", "39.38N", "39.38S", "4.02N", "4.02S", "40.99N", "40.99S",
"42.59N", "42.59S", "44.20N", "44.20S", "45.81N", "45.81S", "47.42N",
"49.03N", "5.63N", "5.63S", "50.63N", "52.24N", "52.24S", "53.84N",
"55.45N", "57.05N", "58.66N", "60.27N", "61.88N", "63.49N", "65.09N",
"68.31N", "69.92N", "7.23N", "7.23S", "8.84N", "8.84S"), class = "factor")
Here is a base R approach with chartr to change "N" to "+" and "S" to "-", and then some string processing with sub to change the "+" and "-" to be in front of the number
tmp <- chartr("NS", "+-", c("10N", "10N", "35S", "25S"))
as.numeric(sub("(\\d+)([-+])", "\\2\\1", tmp))
#[1] 10 10 -35 -25
not that efficient though, (and it assumes that always N/S will be present)
ifelse(grepl("N",df$x), as.numeric(gsub("N","",df$x)), -1*as.numeric(gsub("S","",df$x)))
# [1] 10 20 -35 -25
With gsub and grepl:
as.numeric(gsub('[NS]','',x)) * c(-1,1)[grepl('N',x) + 1]
This results in:
> as.numeric(gsub('[NS]','',x)) * c(-1,1)[grepl('N',x) + 1]
[1] 10 20 -35 -25
Even another option is to use the gsubfn package:
x <- c('10N','20N','35S','25S')
library(gsubfn)
sapply(gsubfn('N|S', list('N'='*1','S'='*-1'), x),
function(x) eval(parse(text = x)),
USE.NAMES = FALSE)
This results in:
> sapply(gsubfn('N|S', list('N'='*1','S'='*-1'), x),
+ function(x) eval(parse(text = x)),
+ USE.NAMES = FALSE)
[1] 10 20 -35 -25
Here is an option with parse_number() from readr package to get the numeric values from the column and endsWith (which is a more efficient version of grepl) to check the trailing letter and make a vector of 1 or -1 based on which letter it is with a little mathematics:
df$x <- with(df, readr::parse_number(x) * (-1) ^ endsWith(x, "S"))
df
# x
#1 10
#2 20
#3 -35
#4 -25

Caculating probability using normal distribution. and t-distribution in R

I have this sample:
x=c(92L, 9L, 38L, 43L, 74L, 16L, 75L, 55L, 39L, 77L, 76L, 52L,
100L, 85L, 62L, 60L, 49L, 28L, 6L, 27L, 63L, 22L, 23L, 99L, 61L,
25L, 19L, 48L, 91L, 57L, 97L, 84L, 31L, 87L, 1L, 21L, 30L, 41L,
13L, 72L, 68L, 95L, 47L, 11L, 24L, 58L, 18L, 67L, 33L, 8L, 50L,
4L, 40L, 12L, 73L, 78L, 86L, 69L, 44L, 83L, 94L, 65L, 37L, 70L,
54L, 46L, 15L, 53L, 89L, 98L, 90L, 3L, 14L, 17L, 42L, 45L, 79L,
20L, 32L, 34L, 64L, 88L, 81L, 96L, 59L, 71L, 56L, 26L, 51L, 29L,
80L, 7L, 36L, 93L, 82L, 35L, 5L, 2L, 10L, 66L)
I want to calculate this probability: P(x) > Mean(x) + 3 assuming that data have normal distribution.
So I do this: mean(x) = 50.5 ; sd(x)=29.01
I generate the density distribution and calculate my probability, which now is:
P(x) > 53.5
pnorm(53.5, mean=mean(x), sd=sd(x), lower.tail=FALSE)
If I want calculating using Standard Distribution:
P(x)>(53.5) = P(z=(x-mean(x)/sd(x))) > ((53.5 - 50.5)/29.01) = P(z)>(3/29.01)
pnorm(3/29.01149, mean=0, sd=1, lower.tail=FALSE)
But when I want to use the T-Student Distribution, how can I proceed?
It is more legitimate to use t distribution here, as standard error is estimated from data.
pt(3 / sd(x), df = length(x) - 1, lower.tail = FALSE)
# [1] 0.4589245
We have length(x) number of data, but also estimate 1 parameter (standard error), so the degree of freedom for t-distribution is length(x) - 1.
There is not much difference compared with using normal distribution, though, given that length(x) is 100 (which is large enough):
pnorm(3 / sd(x), lower.tail = FALSE)
# [1] 0.4588199

Plotting a Dataframe in R

I have a dataframe of the form
Region Name 3-15 4-15 5-15 ... 3-16
Name1 30 82 56 ... 32
Name2 65 23 38 ... 11
... ... ... ... ... ...
Name18 87 33 11 ... 51
The first column being the names of regions and the other columns being recorded events over time (monthly by column)
I'd like to plot the recorded monthly values over time with respect to their associated name. Specifically, a different line for each Named region with a differentiated colour. Any advice would be appreciated, a lot of the plotting functions for data frames seem to function on frames of a different format.
dput() data:
dataframe <- structure(list("LSOA Name" = c("Lancaster 001", "Lancaster 002",
"Lancaster 003", "Lancaster 004", "Lancaster 005", "Lancaster 006",
"Lancaster 008", "Lancaster 009", "Lancaster 010", "Lancaster 011",
"Lancaster 013", "Lancaster 014", "Lancaster 015", "Lancaster 016",
"Lancaster 017", "Lancaster 018", "Lancaster 019", "Lancaster 020"
), "3-15" = c(49L, 16L, 17L, 28L, 21L, 197L, 57L, 143L, 78L,
121L, 67L, 223L, 41L, 86L, 66L, 27L, 40L, 77L), "4-15" = c(63L,
11L, 26L, 29L, 19L, 203L, 69L, 154L, 82L, 125L, 62L, 198L, 44L,
99L, 64L, 26L, 42L, 99L), "5-15" = c(67L, 10L, 20L, 30L, 10L,
194L, 62L, 186L, 61L, 110L, 75L, 273L, 29L, 126L, 92L, 34L, 41L,
88L), "6-15" = c(58L, 8L, 18L, 36L, 29L, 198L, 62L, 167L, 83L,
110L, 59L, 254L, 26L, 99L, 73L, 17L, 30L, 109L), "7-15" = c(53L,
29L, 27L, 23L, 38L, 188L, 56L, 149L, 90L, 129L, 37L, 226L, 32L,
119L, 57L, 14L, 30L, 96L), "8-15" = c(44L, 9L, 25L, 28L, 29L,
237L, 69L, 171L, 78L, 108L, 45L, 261L, 22L, 103L, 68L, 33L, 35L,
108L), "9-15" = c(59L, 12L, 18L, 35L, 19L, 230L, 45L, 128L, 74L,
144L, 56L, 223L, 26L, 90L, 51L, 27L, 23L, 120L), "10-15" = c(45L,
26L, 31L, 23L, 25L, 195L, 53L, 155L, 74L, 120L, 58L, 276L, 38L,
92L, 72L, 25L, 40L, 123L), "11-15" = c(31L, 11L, 33L, 15L, 19L,
188L, 52L, 127L, 66L, 102L, 50L, 241L, 26L, 74L, 72L, 26L, 35L,
68L), "12-15" = c(34L, 22L, 21L, 22L, 17L, 205L, 80L, 150L, 73L,
109L, 50L, 228L, 29L, 57L, 59L, 14L, 45L, 93L), "1-16" = c(20L,
9L, 25L, 21L, 11L, 199L, 46L, 124L, 65L, 117L, 40L, 224L, 28L,
88L, 43L, 22L, 18L, 94L), "2-16" = c(54L, 11L, 29L, 20L, 11L,
164L, 44L, 117L, 70L, 85L, 46L, 192L, 23L, 89L, 50L, 27L, 29L,
86L), "3-16" = c(53L, 11L, 24L, 26L, 19L, 203L, 45L, 144L, 66L,
109L, 47L, 213L, 15L, 120L, 59L, 15L, 33L, 127L)), .Names = c("LSOA Name",
"3-15", "4-15", "5-15", "6-15", "7-15", "8-15", "9-15", "10-15",
"11-15", "12-15", "1-16", "2-16", "3-16"), row.names = c(NA,
-18L), class = "data.frame")
A typical way of plotting lines by groups in ggplot is to shift the data to long format, where one column identifies the group, and the other columns identify the x and y axis values.
This example shifts your data into long format with three columns: LSOAName, month_col, and values_col. It adds a day value onto the month-year, and converts that column to a date. Then it plots a line for each group.
I've renamed your dataframe d, because dataframe could be easily misinterpreted as the function data.frame().
# load libraries
library(magrittr)
library(dplyr)
library(tidyr)
library(ggplot2)
# rename dataframe so it doesn't look so much like the base function
d <- dataframe
# remove spaces in column names
names(d) <- gsub(" ", "", names(d))
# shift data from wide to long and then
# add a day value and convert day-month-year to date class
d %<>% gather(month_col, values_col, -LSOAName) %>%
mutate(month_col = as.Date(paste0("1-", month_col), "%d-%m-%y"))
# plot using ggplot2
ggplot(d, aes(x = month_col, y = values_col, colour = LSOAName)) +
geom_line()
Edit
%<>% is found in the magrittr package. It is a compound pipe assignment operator. While %>% returns the result of a pipeline, %<>% assigns the result back to the left side object.
Instead of writing
d <- d %>% [pipeline]
you can assign the results to d by writing
d %<>% [pipeline]

Select a set of edges which create the largest graph given that some edges are mutually exclusive of others

I'm trying to determine how to best tackle this problem.
Given a set of nodes and multiple, conflicting ways in which they could be connected I need to select the set of non-conflicting relations such that largest number of nodes remain in connected.
Example.
Here is a graph including all possible relations (edges) ignoring conflicts. Eg., this image doesn't depict the dependence of the edges on each other.
All edges attached to a specific node are dependent on one another. For simplicity each edge implies an attribute to each node it connects say A...Z. If an edge connecting nodes 3 and 16 specifies attributes 3-B and 16-F, then all edges connecting 16 to other nodes must have attribute 16-F. Similarly all edges connecting 3 to other nodes must have attribute 3-B.
Here is the same graph when specifying attribute F to node 16. This attribute removes most edges leaving one edge connecting 16-4 and one edge connecting 16-3. This has left no edges between 16-42.
(16 is near the left in both images.)
This image does not illustrate that the edge connecting 3-42 will specify an attribute for node 42, say 42-X. This will further constrain connections to 42 and further break up the graph. I have not displayed this because this is what my question pertains to.
I am looking for advice.
Is this a known problem? Can you point me to any references?
How
would you approach this problem? My best idea is to iterate,
starting at each edge, over all possible attributes. Evaluate each
partitioning and find which preserves the largest network. This
sounds challenging though and I could use some help.
If this is the solution is there a way using igraph in R to specify an "edge attribute constraint" and pull out the resulting, fragmented graph.
I have dput the graph here:
df = structure(list(nodeA = c(3L, 4L, 42L, 43L, 44L, 29L, 30L, 29L, 30L, 3L, 4L, 6L, 43L, 44L, 43L, 44L, 29L, 30L, 29L, 30L, 52L, 29L, 30L, 35L, 25L, 35L, 25L, 43L, 44L, 29L, 30L, 3L, 4L, 43L, 44L, 29L, 30L, 25L, 29L, 30L, 42L, 3L, 4L, 17L, 43L, 44L, 29L, 30L, 29L, 30L, 17L, 17L, 29L, 30L, 6L, 43L, 44L, 29L, 30L, 52L, 35L, 35L, 25L, 25L, 24L, 24L, 43L, 44L, 29L, 30L, 35L, 35L, 25L, 25L, 24L, 24L, 43L, 44L, 29L, 30L, 35L, 35L, 25L, 25L, 24L, 24L, 52L, 42L, 3L, 42L, 42L, 3L, 4L, 42L, 25L, 42L, 25L, 3L, 4L, 42L, 3L, 4L, 17L, 35L, 3L, 4L, 35L, 43L, 44L, 29L, 30L, 35L, 35L, 35L, 52L, 25L, 25L, 24L, 24L, 35L, 29L, 30L, 3L, 4L, 43L, 44L, 29L, 30L, 25L, 29L, 30L, 52L, 43L, 44L, 29L, 30L, 25L, 29L, 30L, 3L, 4L, 43L, 44L, 29L, 30L, 52L, 43L, 44L, 43L, 44L, 29L, 30L, 3L, 4L, 43L, 44L, 29L, 30L, 52L, 52L, 43L, 44L, 29L, 30L, 35L, 52L, 52L, 3L, 4L, 43L, 44L, 29L, 30L, 52L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 17L, 17L, 42L, 42L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 3L, 4L, 25L, 25L, 16L, 16L, 3L, 4L, 43L, 44L, 24L, 3L, 4L, 52L, 52L, 17L, 35L, 35L, 35L, 17L, 3L, 4L, 6L, 35L, 42L, 42L, 42L, 42L, 3L, 4L, 17L, 25L, 17L, 17L, 29L, 30L, 25L, 3L, 4L, 29L, 30L, 3L, 4L, 17L, 17L, 17L, 35L, 3L, 4L, 17L, 17L, 17L, 29L, 30L, 43L, 44L, 43L, 44L, 29L, 30L, 17L, 6L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 3L, 43L, 44L, 29L, 30L, 3L, 43L, 44L, 29L, 30L, 17L, 17L, 42L, 42L, 25L, 42L, 25L, 43L, 44L, 29L, 30L, 42L, 17L, 17L, 42L, 42L, 43L, 44L, 29L, 30L, 25L, 29L, 30L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 25L, 29L, 30L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 43L, 44L, 29L, 30L, 25L, 25L, 25L, 25L), nodeB = c(16L, 16L, 17L, 24L, 24L, 25L, 25L, 35L, 35L, 16L, 16L, 17L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 35L, 35L, 35L, 43L, 43L, 44L, 44L, 24L, 24L, 25L, 25L, 16L, 16L, 24L, 24L, 25L, 25L, 35L, 35L, 35L, 16L, 16L, 16L, 24L, 24L, 24L, 25L, 25L, 35L, 35L, 43L, 44L, 52L, 52L, 17L, 24L, 24L, 25L, 25L, 35L, 43L, 44L, 29L, 30L, 43L, 44L, 24L, 24L, 25L, 25L, 43L, 44L, 29L, 30L, 43L, 44L, 24L, 24L, 25L, 25L, 43L, 44L, 29L, 30L, 43L, 44L, 17L, 24L, 42L, 43L, 44L, 16L, 16L, 17L, 35L, 17L, 35L, 16L, 16L, 52L, 16L, 16L, 6L, 25L, 16L, 16L, 52L, 24L, 24L, 25L, 25L, 43L, 44L, 25L, 25L, 29L, 30L, 43L, 44L, 17L, 42L, 42L, 16L, 16L, 24L, 24L, 25L, 25L, 35L, 35L, 35L, 35L, 24L, 24L, 25L, 25L, 35L, 35L, 35L, 16L, 16L, 24L, 24L, 25L, 25L, 35L, 17L, 17L, 24L, 24L, 25L, 25L, 16L, 16L, 24L, 24L, 25L, 25L, 25L, 35L, 24L, 24L, 25L, 25L, 25L, 29L, 30L, 16L, 16L, 24L, 24L, 25L, 25L, 35L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 43L, 44L, 3L, 4L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 16L, 16L, 35L, 35L, 3L, 4L, 16L, 16L, 17L, 17L, 17L, 16L, 16L, 29L, 30L, 6L, 25L, 29L, 30L, 42L, 16L, 16L, 25L, 52L, 16L, 16L, 16L, 16L, 16L, 16L, 24L, 35L, 43L, 44L, 52L, 52L, 35L, 16L, 16L, 52L, 52L, 16L, 16L, 24L, 43L, 44L, 25L, 16L, 16L, 24L, 43L, 44L, 52L, 52L, 17L, 17L, 24L, 24L, 25L, 25L, 52L, 42L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 42L, 24L, 24L, 25L, 25L, 42L, 24L, 24L, 25L, 25L, 43L, 44L, 4L, 17L, 35L, 17L, 35L, 24L, 24L, 25L, 25L, 16L, 43L, 44L, 4L, 4L, 24L, 24L, 25L, 25L, 35L, 35L, 35L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 35L, 35L, 35L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 24L, 24L, 25L, 25L, 35L, 35L, 35L, 35L), attributeA = c(25L, 25L, 130L, 110L, 110L, 110L, 110L, 113L, 113L, 43L, 43L, 71L, 5L, 5L, 127L, 127L, 5L, 5L, 127L, 127L, 72L, 130L, 130L, 137L, 140L, 137L, 140L, 6L, 6L, 6L, 6L, 56L, 56L, 137L, 137L, 137L, 137L, 130L, 140L, 140L, 29L, 68L, 68L, 56L, 143L, 143L, 143L, 143L, 146L, 146L, 43L, 43L, 45L, 45L, 46L, 80L, 80L, 80L, 80L, 47L, 11L, 11L, 80L, 80L, 80L, 80L, 84L, 84L, 84L, 84L, 14L, 14L, 84L, 84L, 84L, 84L, 90L, 90L, 90L, 90L, 18L, 18L, 90L, 90L, 90L, 90L, 110L, 37L, 122L, 114L, 114L, 108L, 108L, 58L, 27L, 136L, 109L, 26L, 26L, 115L, 111L, 111L, 78L, 109L, 112L, 112L, 78L, 114L, 114L, 114L, 114L, 37L, 37L, 47L, 73L, 114L, 114L, 114L, 114L, 128L, 111L, 111L, 125L, 125L, 54L, 54L, 54L, 54L, 45L, 58L, 58L, 143L, 55L, 55L, 55L, 55L, 126L, 136L, 136L, 44L, 44L, 56L, 56L, 56L, 56L, 145L, 68L, 68L, 57L, 57L, 57L, 57L, 128L, 128L, 58L, 58L, 58L, 58L, 143L, 146L, 59L, 59L, 59L, 59L, 126L, 70L, 70L, 129L, 129L, 60L, 60L, 60L, 60L, 73L, 61L, 61L, 61L, 61L, 62L, 62L, 62L, 62L, 124L, 124L, 91L, 91L, 63L, 63L, 63L, 63L, 64L, 64L, 64L, 64L, 65L, 65L, 65L, 65L, 135L, 135L, 58L, 136L, 127L, 127L, 57L, 57L, 143L, 143L, 68L, 138L, 138L, 143L, 143L, 80L, 136L, 126L, 126L, 109L, 139L, 139L, 128L, 80L, 110L, 112L, 113L, 30L, 141L, 141L, 135L, 70L, 125L, 125L, 126L, 126L, 142L, 69L, 69L, 128L, 128L, 144L, 144L, 138L, 128L, 128L, 142L, 145L, 145L, 139L, 129L, 129L, 130L, 130L, 121L, 121L, 79L, 79L, 79L, 79L, 91L, 109L, 82L, 82L, 82L, 82L, 86L, 86L, 86L, 86L, 88L, 88L, 88L, 88L, 97L, 92L, 92L, 92L, 92L, 118L, 94L, 94L, 94L, 94L, 107L, 107L, 89L, 138L, 111L, 140L, 113L, 116L, 116L, 116L, 116L, 1L, 134L, 134L, 92L, 19L, 135L, 135L, 135L, 135L, 128L, 138L, 138L, 136L, 136L, 136L, 136L, 137L, 137L, 137L, 137L, 130L, 140L, 140L, 138L, 138L, 138L, 138L, 139L, 139L, 139L, 139L, 140L, 140L, 140L, 140L, 138L, 140L, 144L, 146L), attributeB = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 23L, 23L, 23L, 23L, 24L, 24L, 25L, 25L, 25L, 27L, 27L, 28L, 28L, 29L, 29L, 29L, 36L, 36L, 36L, 36L, 36L, 36L, 37L, 37L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L, 44L, 44L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 48L, 48L, 48L, 48L, 49L, 49L, 49L, 49L, 49L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 51L, 51L, 51L, 51L, 52L, 52L, 52L, 52L, 54L, 54L, 54L, 55L, 56L, 56L, 56L, 56L, 56L, 56L, 57L, 58L, 58L, 58L, 58L, 59L, 59L, 59L, 59L, 59L, 60L, 60L, 60L, 60L, 62L, 63L, 64L, 65L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 67L, 68L, 68L, 68L, 68L, 70L, 70L, 70L, 70L, 70L, 71L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 77L, 77L, 78L, 78L, 78L, 78L, 79L, 80L, 81L, 81L, 81L, 81L, 85L, 85L, 85L, 85L, 87L, 87L, 87L, 87L, 89L, 91L, 91L, 91L, 91L, 92L, 93L, 93L, 93L, 93L, 96L, 96L, 97L, 108L, 108L, 110L, 110L, 115L, 115L, 115L, 115L, 117L, 117L, 117L, 118L, 122L, 125L, 125L, 125L, 125L, 125L, 125L, 125L, 126L, 126L, 126L, 126L, 127L, 127L, 127L, 127L, 127L, 127L, 127L, 128L, 128L, 128L, 128L, 129L, 129L, 129L, 129L, 130L, 130L, 130L, 130L, 135L, 137L, 141L, 143L)), .Names = c("nodeA", "nodeB", "attributeA", "attributeB" ), row.names = c(3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 13L, 18L, 19L, 20L, 24L, 25L, 26L, 27L, 28L, 29L, 31L, 32L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 52L, 53L, 54L, 55L, 59L, 60L, 62L, 63L, 64L, 65L, 71L, 72L, 73L, 78L, 82L, 83L, 86L, 87L, 88L, 89L, 90L, 96L, 97L, 98L, 99L, 108L, 109L, 112L, 114L, 115L, 116L, 117L, 120L, 121L, 122L, 129L, 131L, 134L, 135L, 141L, 142L, 143L, 144L, 146L, 147L, 153L, 154L, 156L, 157L, 163L, 164L, 165L, 166L, 168L, 169L, 175L, 176L, 178L, 179L, 183L, 186L, 187L, 188L, 189L, 196L, 197L, 198L, 201L, 204L, 206L, 208L, 209L, 213L, 216L, 217L, 221L, 222L, 225L, 226L, 230L, 241L, 242L, 243L, 244L, 248L, 249L, 255L, 256L, 259L, 260L, 264L, 265L, 272L, 276L, 277L, 284L, 285L, 287L, 288L, 289L, 290L, 292L, 293L, 294L, 295L, 303L, 304L, 305L, 306L, 308L, 309L, 310L, 315L, 316L, 318L, 319L, 320L, 321L, 325L, 333L, 334L, 336L, 337L, 338L, 339L, 347L, 348L, 350L, 351L, 352L, 353L, 354L, 359L, 365L, 366L, 367L, 368L, 369L, 373L, 374L, 381L, 382L, 384L, 385L, 386L, 387L, 390L, 395L, 396L, 397L, 398L, 406L, 407L, 408L, 409L, 411L, 412L, 416L, 417L, 421L, 422L, 423L, 424L, 430L, 431L, 432L, 433L, 438L, 439L, 440L, 441L, 447L, 448L, 450L, 452L, 454L, 455L, 456L, 457L, 458L, 459L, 468L, 472L, 473L, 476L, 477L, 481L, 483L, 484L, 485L, 488L, 493L, 494L, 495L, 501L, 504L, 508L, 511L, 512L, 513L, 514L, 516L, 518L, 519L, 520L, 523L, 524L, 526L, 528L, 529L, 534L, 535L, 538L, 539L, 540L, 543L, 544L, 550L, 555L, 556L, 558L, 561L, 562L, 564L, 565L, 576L, 577L, 582L, 583L, 584L, 585L, 590L, 594L, 596L, 597L, 598L, 599L, 605L, 606L, 607L, 608L, 613L, 614L, 615L, 616L, 620L, 622L, 623L, 624L, 625L, 629L, 631L, 632L, 633L, 634L, 643L, 644L, 647L, 657L, 660L, 665L, 666L, 673L, 674L, 675L, 676L, 691L, 692L, 693L, 696L, 700L, 705L, 706L, 707L, 708L, 711L, 712L, 713L, 720L, 721L, 722L, 723L, 728L, 729L, 730L, 731L, 733L, 734L, 735L, 741L, 742L, 743L, 744L, 750L, 751L, 752L, 753L, 759L, 760L, 761L, 762L, 772L, 777L, 787L, 790L), class = "data.frame")
library(igraph)
g = graph.data.frame(df)
plot(g, vertex.size = 6, edge.arrow.mode=1, edge.arrow.size = 0)
> head(df)
nodeA nodeB attributeA attributeB
1 3 16 25 1
4 4 16 25 1
5 42 17 130 1
7 43 24 110 1
8 44 24 110 1
9 29 25 110 1
In the above, row 1 attributeA is the exclusive attribute for node 3 such that all other edges connecting to node 3 must have attribute 25. Similarly, attributeB indicates that all edges connecting to node 16 must have the attribute 1. It is not necessary that row 1 be an edge, but it is necessary that no retained edges conflict.
Thanks for reading!
Is this a known problem? Can you point me to any references?
This is quite an interesting problem, and not one that I've encountered before.
How would you approach this problem?
I would approach this problem from an integer programming perspective. The decision variables will be used to select the attribute of each node (only edges labeled with the attributes of both of their endpoints will be allowed). Further, we will select a "root node" that we expect to be in the large connected component, and we will create flow outward from this root node. Each other node will have demand 1, and flow will only be possible over valid edges. We will maximize the amount of flow pushed out from the root node; this will be the number of other nodes in the large component.
To achieve this formulation, I would create two classes of variables:
Node attribute variables: For each node i and attribute a, I would create a binary variable z_ia that is 1 if node i is assigned attribute a and 0 otherwise.
Flow variables: For each edge from node i to j (I assume "from" is nodeA in your data frame and "to" is nodeB in your data frame), variable x_ij indicates the amount of flow from i to j (negative values indicate flow from j to i).
We also have a number of different constraints:
Each node only has 1 attribute: This can be achieved with \sum_{a\in A} z_ia = 1 for each node i, where A is the set of all attributes.
Edge flows are 0 if the edge is not valid: For each edge from i to j with attributes a and b, respectively, we will have x_ij <= n*z_ia, x_ij <= n*z_jb, x_ij >= -n*z_ia, and x_ij >= -n*z_jb. In all four constraints, n is the total number of nodes. These constraints will force x_ij=0 if z_ia=0 or z_jb=0, and otherwise will not be binding.
The net flow to any non-root node falls in [0, 1]: This constraint ensures that all outflow must come from the root, so nodes can only get flow if they are connected to the root. For each non-root node i with edges incoming from node set I and edges outgoing to node set O, these constraints are of the form \sum_{j\in I} x_ji - \sum_{j\in O} x_ij >= 0 and \sum_{j\in I} x_ji - \sum_{j\in O} x_ij <= 1.
The objective is to maximize the amount of flow out of the root node r. If r has incoming edges from nodes in set I and outgoing edges to nodes in set O, then this objective (which we maximize) is \sum_{j\in O} x_ji - \sum_{j\in I} x_ij.
With these variables and constraints in place, all you need to do is specify the root node r and solve; the solution will indicate the best possible assignment of attributes to nodes, assuming that r is in the largest component. If you re-solved for each root node r, you would end up with the global optimal assignment.
The following in an implementation of this approach with the lpSolve package in R:
library(lpSolve)
optim <- function(df, r) {
# Some book keeping
nodes = c(df$nodeA, df$nodeB)
u.nodes <- unique(nodes)
if (!r %in% u.nodes) {
stop("Invalid root node provided")
}
n.node <- length(u.nodes)
attrs = c(df$attributeA, df$attributeB)
node.attrs <- do.call(rbind, lapply(u.nodes, function(x) {
data.frame(node=x, attr=unique(attrs[nodes == x]))
}))
n.na <- nrow(node.attrs)
n.e <- nrow(df)
# Constraints limiting each node to have exactly one attribute
node.one.attr <- t(sapply(u.nodes, function(i) {
c(node.attrs$node == i, rep(0, 2*n.e))
}))
node.one.attr.dir <- rep("==", n.node)
node.one.attr.rhs <- rep(1, n.node)
# Constraints limiting edges to only be used if both attributes are selected
edge.flow <- do.call(rbind, lapply(seq_len(n.e), function(idx) {
i <- df$nodeA[idx]
j <- df$nodeB[idx]
a <- df$attributeA[idx]
b <- df$attributeB[idx]
na.i <- node.attrs$node == i & node.attrs$attr == a
na.j <- node.attrs$node == j & node.attrs$attr == b
rbind(c(-n.node*na.i, seq_len(n.e) == idx, -(seq_len(n.e) == idx)),
c(-n.node*na.j, seq_len(n.e) == idx, -(seq_len(n.e) == idx)),
c(n.node*na.i, seq_len(n.e) == idx, -(seq_len(n.e) == idx)),
c(n.node*na.j, seq_len(n.e) == idx, -(seq_len(n.e) == idx)))
}))
edge.flow.dir <- rep(c("<=", "<=", ">=", ">="), n.e)
edge.flow.rhs <- rep(0, 4*n.e)
# Constraints limiting net flow on non-root nodes
net.flow <- do.call(rbind, lapply(u.nodes, function(i) {
if (i == r) {
return(NULL)
}
rbind(c(rep(0, n.na), (df$nodeB == i) - (df$nodeA == i),
-(df$nodeB == i) + (df$nodeA == i)),
c(rep(0, n.na), (df$nodeB == i) - (df$nodeA == i),
-(df$nodeB == i) + (df$nodeA == i)))
}))
net.flow.dir <- rep(c(">=", "<="), n.node-1)
net.flow.rhs <- rep(c(0, 1), n.node-1)
# Build the model
mod <- lp(direction = "max",
objective.in = c(rep(0, n.na), (df$nodeA == r) - (df$nodeB == r),
-(df$nodeA == r) + (df$nodeB == r)),
const.mat = rbind(node.one.attr, edge.flow, net.flow),
const.dir = c(node.one.attr.dir, edge.flow.dir, net.flow.dir),
const.rhs = c(node.one.attr.rhs, edge.flow.rhs, net.flow.rhs),
binary.vec = seq_len(n.na))
opt <- node.attrs[mod$solution[1:n.na] > 0.999,]
valid.edges <- df[opt$attr[match(df$nodeA, opt$node)] == df$attributeA &
opt$attr[match(df$nodeB, opt$node)] == df$attributeB,]
list(attrs = opt,
edges = valid.edges,
objval = mod$objval)
}
It can solve the problem for subsets of the nodes in your original graph, but it becomes quite slow as you include an increasing number of nodes:
# Limit to 5 nodes
keep <- c(3, 4, 6, 16, 42)
df.play <- df[df$nodeA %in% keep & df$nodeB %in% keep,]
(opt.play <- optim(df.play, 42))
# $attrs
# node attr
# 24 3 50
# 45 4 50
# 50 42 91
# 60 16 127
# 87 6 109
#
# $edges
# nodeA nodeB attributeA attributeB
# 416 42 3 91 50
# 417 42 4 91 50
#
# $objval
# [1] 2
That run took 15 seconds. To speed this up, you could consider switching to a more powerful solver such as cplex or gurobi. These solvers are free for academic use but non-free otherwise.
If this is the solution is there a way using igraph in R to specify an "edge attribute constraint" and pull out the resulting, fragmented graph.
Yes, given the attributes you can easily subset and plot the graph. For the 5-node example that I solved above:
g <- graph.data.frame(opt.play$edges, vertices=unique(c(df.play$nodeA, df.play$nodeB)))
plot(g, vertex.size = 6, edge.arrow.mode=1, edge.arrow.size = 0)
While working through this problem I stumbled upon a simpler solution. It seems my formulation of the problem was making the answer hard to see.
The core of the matter is: when two different constraints are applied to a node it effectively becomes two distinct nodes.
Framing the challenge in this way allows us to rapidly construct graphs for each set of constraints. We can then quickly inspect these, look at the size, and (as my original question desired) select the set of constraints which preserves the largest graph.
g = graph.data.frame(df); plot(g, vertex.size = 6, edge.arrow.mode=1, edge.arrow.size = 0)
# Combine the node and the rule into a new, unique node id referencing both the node and the constraint
df.split = c(df[,1:2]) + df[,3:4]*1E3
# Keep track of edge numbers in this dataset for later
df.split = cbind(df.split, row = seq(nrow(df)))
g.split = graph.data.frame(df.split); plot(g.split, vertex.size = 6, edge.arrow.mode=1, edge.arrow.size = 0)
# Decompose into unlinked sub graphs and count the edges in each
g.list = decompose.graph(g.split)
g.list.nodenum = sapply(g.list, ecount)
head(g.list.nodenum[order(g.list.nodenum, decreasing=T)])
[1] 9 8 5 5 5 5
# Select the largest subgraph
g.sub = g.list[[order(g.list.nodenum, decreasing=T)[1]]]
plot(g.sub)
# Find what edges these were in the original dataset
originaledges = E(g.sub)$row
originaledges
[1] 129 157 130 158 131 159 212 213 132
# Play with the resulting graph, the largest graph which obeys constraints at all nodes.
df.largest = df[originaledges,]
df.largest
nodeA nodeB attributeA attributeB
292 25 35 45 41
352 29 25 58 45
293 29 35 58 41
353 30 25 58 45
294 30 35 58 41
354 52 25 143 45
476 52 29 143 58
477 52 30 143 58
295 52 35 143 41
g.largest = graph.data.frame(df.largest); plot(g.largest, vertex.size = 6, edge.arrow.mode=1, edge.arrow.size = 0)
Hopefully this helps someone someday!

Resources