Nesting Functions in R - r
I am relatively new to R; and, I need help with a user defined function. I would like to see where each observation of a data frame ranks in a subset of similar observations of the same data frame. I'm having trouble referencing the original observation, in order to extract its rank, within my function.
Here is a sample of my data:
> dput(df)
structure(list(Name = c("Alex Abrines", "Steven Adams", "Cole Aldrich",
"LaMarcus Aldridge", "Kyle Anderson", "Ryan Anderson", "Giannis Antetokounmpo",
"Carmelo Anthony", "OG Anunoby", "Darrell Arthur", "Will Barton",
"Bradley Beal", "Davis Bertans", "Nemanja Bjelica", "Malcolm Brogdon",
"Aaron Brooks", "Dillon Brooks", "Lorenzo Brown", "Sterling Brown",
"Reggie Bullock", "Jimmy Butler", "Dwight Buycks", "Clint Capela",
"Wilson Chandler", "Torrey Craig", "Jamal Crawford", "Deyonta Davis",
"Matthew Dellavedova", "DeMar DeRozan", "Gorgui Dieng", "Andre Drummond",
"James Ennis", "Kenneth Faried", "Raymond Felton", "Terrance Ferguson",
"Bryn Forbes", "Tim Frazier", "Langston Galloway", "Marc Gasol",
"Pau Gasol", "Paul George", "Marcus Georges-Hunt", "Taj Gibson",
"Manu Ginobili", "Marcin Gortat", "Jerami Grant", "Danny Green",
"Gerald Green", "JaMychal Green", "Blake Griffin", "James Harden",
"Gary Harris", "Andrew Harrison", "Myke Henry", "John Henson",
"Nene Hilario", "Darrun Hilliard", "Josh Huestis", "Serge Ibaka",
"Stanley Johnson", "Nikola Jokic", "Tyus Jones", "Luke Kennard",
"Sean Kilpatrick", "Joffrey Lauvergne", "Kyle Lowry", "Trey Lyles",
"Ian Mahinmi", "Thon Maker", "Jarell Martin", "Luc Mbah a Moute",
"Ben McLemore", "Jodie Meeks", "Khris Middleton", "Patty Mills",
"Eric Moreland", "Markieff Morris", "Emmanuel Mudiay", "Shabazz Muhammad",
"Xavier Munford", "Dejounte Murray", "Jamal Murray", "Lucas Nogueira",
"Kelly Oubre", "Tony Parker", "Patrick Patterson", "Brandon Paul",
"Chris Paul", "Marshall Plumlee", "Jakob Poeltl", "Otto Porter",
"Norman Powell", "Willie Reed", "Tomas Satoransky", "Mike Scott",
"Wayne Selden", "Pascal Siakam", "Ish Smith", "Tony Snell", "Jeff Teague",
"Anthony Tolliver", "Karl-Anthony Towns", "P.J. Tucker", "Jonas Valanciunas",
"Rashad Vaughn", "Russell Westbrook", "Andrew Wiggins", "D.J. Wilson",
"Delon Wright"), Pos = structure(c(5L, 1L, 1L, 1L, 3L, 2L, 3L,
2L, 2L, 2L, 4L, 4L, 2L, 2L, 4L, 4L, 5L, 4L, 4L, 5L, 3L, 4L, 1L,
2L, 5L, 4L, 1L, 4L, 5L, 1L, 1L, 2L, 2L, 4L, 5L, 4L, 4L, 4L, 1L,
1L, 2L, 4L, 2L, 4L, 1L, 2L, 5L, 5L, 2L, 2L, 4L, 4L, 4L, 2L, 1L,
1L, 4L, 2L, 1L, 2L, 1L, 4L, 4L, 4L, 1L, 4L, 2L, 1L, 1L, 2L, 2L,
4L, 4L, 3L, 4L, 1L, 2L, 4L, 3L, 4L, 4L, 4L, 1L, 2L, 4L, 2L, 4L,
4L, 1L, 1L, 2L, 4L, 1L, 4L, 2L, 5L, 2L, 4L, 5L, 4L, 1L, 1L, 2L,
1L, 4L, 4L, 3L, 2L, 4L), .Label = c("C", "PF", "SF", "PG", "SG"
), class = "factor"), Date = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "2018-02-01 *", class = "factor"),
Tm = structure(c(7L, 7L, 6L, 8L, 8L, 3L, 5L, 7L, 9L, 1L,
1L, 10L, 8L, 6L, 5L, 6L, 4L, 9L, 5L, 2L, 6L, 2L, 3L, 1L,
1L, 6L, 4L, 5L, 9L, 6L, 2L, 4L, 1L, 7L, 7L, 8L, 10L, 2L,
4L, 8L, 7L, 6L, 6L, 8L, 10L, 7L, 8L, 3L, 4L, 2L, 3L, 1L,
4L, 4L, 5L, 3L, 8L, 7L, 9L, 2L, 1L, 6L, 2L, 5L, 8L, 9L, 1L,
10L, 5L, 4L, 3L, 4L, 10L, 5L, 8L, 2L, 10L, 1L, 6L, 5L, 8L,
1L, 9L, 10L, 8L, 7L, 8L, 3L, 5L, 9L, 10L, 9L, 2L, 10L, 10L,
4L, 9L, 2L, 5L, 6L, 2L, 6L, 3L, 9L, 5L, 7L, 6L, 5L, 9L), .Label = c("DEN",
"DET", "HOU", "MEM", "MIL", "MIN", "OKC", "SAS", "TOR", "WAS"
), class = "factor"), Opp = structure(c(1L, 1L, 5L, 3L, 3L,
8L, 6L, 1L, 10L, 7L, 7L, 9L, 3L, 5L, 6L, 5L, 2L, 10L, 6L,
4L, 5L, 4L, 8L, 7L, 7L, 5L, 2L, 6L, 10L, 5L, 4L, 2L, 7L,
1L, 1L, 3L, 9L, 4L, 2L, 3L, 1L, 5L, 5L, 3L, 9L, 1L, 3L, 8L,
2L, 4L, 8L, 7L, 2L, 2L, 6L, 8L, 3L, 1L, 10L, 4L, 7L, 5L,
4L, 6L, 3L, 10L, 7L, 9L, 6L, 2L, 8L, 2L, 9L, 6L, 3L, 4L,
9L, 7L, 5L, 6L, 3L, 7L, 10L, 9L, 3L, 1L, 3L, 8L, 6L, 10L,
9L, 10L, 4L, 9L, 9L, 2L, 10L, 4L, 6L, 5L, 4L, 5L, 8L, 10L,
6L, 1L, 5L, 6L, 10L), .Label = c("DEN", "DET", "HOU", "MEM",
"MIL", "MIN", "OKC", "SAS", "TOR", "WAS"), class = "factor"),
MP = c(29L, 32L, 3L, 34L, 30L, 29L, 36L, 34L, 21L, 1L, 36L,
38L, 13L, 14L, 10L, 3L, 32L, 11L, 24L, 35L, 40L, 19L, 35L,
34L, 22L, 17L, 15L, 25L, 38L, 13L, 28L, 15L, 10L, 14L, 4L,
18L, 17L, 4L, 33L, 20L, 36L, 6L, 33L, 20L, 26L, 25L, 28L,
30L, 20L, 35L, 37L, 38L, 34L, 22L, 32L, 13L, 8L, 12L, 35L,
36L, 37L, 17L, 21L, 18L, 2L, 35L, 15L, 19L, 13L, 28L, 35L,
10L, 9L, 35L, 24L, 5L, 32L, 14L, 3L, 7L, 24L, 34L, 3L, 23L,
17L, 15L, 2L, 30L, 5L, 16L, 29L, 26L, 5L, 28L, 19L, 31L,
13L, 29L, 29L, 28L, 22L, 33L, 31L, 29L, 4L, 39L, 30L, 4L,
13L), Player.ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 42L, 41L, 43L,
44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 53L, 52L, 54L, 55L,
56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L,
80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L,
103L, 104L, 105L, 106L, 107L, 108L, 109L), .Label = c("abrinal01",
"adamsst01", "aldrico01", "aldrila01", "anderky01", "anderry01",
"antetgi01", "anthoca01", "anunoog01", "arthuda01", "bartowi01",
"bealbr01", "bertada01", "bjeline01", "brogdma01", "brookaa01",
"brookdi01", "brownlo01", "brownst02", "bullore01", "butleji01",
"buyckdw01", "capelca01", "chandwi01", "craigto01", "crawfja01",
"davisde01", "dellama01", "derozde01", "dienggo01", "drumman01",
"ennisja01", "farieke01", "feltora01", "fergute01", "forbebr01",
"fraziti01", "gallola01", "gasolma01", "gasolpa01", "georgma01",
"georgpa01", "gibsota01", "ginobma01", "gortama01", "grantje01",
"greenda02", "greenge01", "greenja01", "griffbl01", "hardeja01",
"harrian01", "harriga01", "henrymy01", "hensojo01", "hilarne01",
"hillida01", "huestjo01", "ibakase01", "johnsst04", "jokicni01",
"jonesty01", "kennalu01", "kilpase01", "lauvejo01", "lowryky01",
"lylestr01", "mahinia01", "makerth01", "martija01", "mbahalu01",
"mclembe01", "meeksjo01", "middlkh01", "millspa02", "moreler01",
"morrima02", "mudiaem01", "muhamsh01", "munfoxa02", "murrade01",
"murraja01", "noguelu01", "oubreke01", "parketo01", "pattepa01",
"paulbr01", "paulch01", "plumlma02", "poeltja01", "porteot01",
"powelno01", "reedwi02", "satorto01", "scottmi01", "seldewa01",
"siakapa01", "smithis01", "snellto01", "teaguje01", "tollian01",
"townska01", "tuckepj01", "valanjo01", "vaughra01", "westbru01",
"wiggian01", "wilsodj01", "wrighde01"), class = "factor"),
Game.ID = structure(c(7L, 7L, 6L, 8L, 8L, 3L, 5L, 7L, 9L,
1L, 1L, 10L, 8L, 6L, 5L, 6L, 4L, 9L, 5L, 2L, 6L, 2L, 3L,
1L, 1L, 6L, 4L, 5L, 9L, 6L, 2L, 4L, 1L, 7L, 7L, 8L, 10L,
2L, 4L, 8L, 7L, 6L, 6L, 8L, 10L, 7L, 8L, 3L, 4L, 2L, 3L,
1L, 4L, 4L, 5L, 3L, 8L, 7L, 9L, 2L, 1L, 6L, 2L, 5L, 8L, 9L,
1L, 10L, 5L, 4L, 3L, 4L, 10L, 5L, 8L, 2L, 10L, 1L, 6L, 5L,
8L, 1L, 9L, 10L, 8L, 7L, 8L, 3L, 5L, 9L, 10L, 9L, 2L, 10L,
10L, 4L, 9L, 2L, 5L, 6L, 2L, 6L, 3L, 9L, 5L, 7L, 6L, 5L,
9L), .Label = c("2018-02-01 * DEN", "2018-02-01 * DET", "2018-02-01 * HOU",
"2018-02-01 * MEM", "2018-02-01 * MIL", "2018-02-01 * MIN",
"2018-02-01 * OKC", "2018-02-01 * SAS", "2018-02-01 * TOR",
"2018-02-01 * WAS"), class = "factor")), .Names = c("Name",
"Pos", "Date", "Tm", "Opp", "MP", "Player.ID", "Game.ID"), class = "data.frame", row.names = c(NA,
109L))
I would like to write a function that, for each observation:
> df[1, ]
Name Pos Date Tm Opp MP Player.ID Game.ID
1 Alex Abrines SG 2018-02-01 * OKC DEN 29 abrinal01 2018-02-01 * OKC
creates a subset of all other observations with a matching df$Game.ID.
> df[df$Game.ID == '2018-02-01 * OKC', ]
Name Pos Date Tm Opp MP Player.ID Game.ID
1 Alex Abrines SG 2018-02-01 * OKC DEN 29 abrinal01 2018-02-01 * OKC
2 Steven Adams C 2018-02-01 * OKC DEN 32 adamsst01 2018-02-01 * OKC
8 Carmelo Anthony PF 2018-02-01 * OKC DEN 34 anthoca01 2018-02-01 * OKC
34 Raymond Felton PG 2018-02-01 * OKC DEN 14 feltora01 2018-02-01 * OKC
35 Terrance Ferguson SG 2018-02-01 * OKC DEN 4 fergute01 2018-02-01 * OKC
41 Paul George PF 2018-02-01 * OKC DEN 36 georgpa01 2018-02-01 * OKC
46 Jerami Grant PF 2018-02-01 * OKC DEN 25 grantje01 2018-02-01 * OKC
58 Josh Huestis PF 2018-02-01 * OKC DEN 12 huestjo01 2018-02-01 * OKC
86 Patrick Patterson PF 2018-02-01 * OKC DEN 15 pattepa01 2018-02-01 * OKC
106 Russell Westbrook PG 2018-02-01 * OKC DEN 39 westbru01 2018-02-01 * OKC
and then returns the rank of the original observation's df$MP
> df[1, c('MP')]
[1] 29
in the hierarchy of the new subset.
> xx <- data.frame(cbind(sort(df[df$Game.ID == '2018-02-01 * OKC', c('MP')], decreasing = TRUE), rownames(data.table(sort(df[df$Game.ID == '2018-02-01 * OKC', c('MP')], decreasing = TRUE)))))
> xx
X1 X2
1 39 1
2 36 2
3 34 3
4 32 4
5 29 5
6 25 6
7 15 7
8 14 8
9 12 9
10 4 10
> colnames(xx) <- c('MP', 'Depth.Chart')
> yy <- df[df$Game.ID == '2018-02-01 * OKC', ]
> yy
Name Pos Date Tm Opp MP Player.ID
1 Alex Abrines SG 2018-02-01 * OKC DEN 29 abrinal01
2 Steven Adams C 2018-02-01 * OKC DEN 32 adamsst01
8 Carmelo Anthony PF 2018-02-01 * OKC DEN 34 anthoca01
34 Raymond Felton PG 2018-02-01 * OKC DEN 14 feltora01
35 Terrance Ferguson SG 2018-02-01 * OKC DEN 4 fergute01
41 Paul George PF 2018-02-01 * OKC DEN 36 georgpa01
46 Jerami Grant PF 2018-02-01 * OKC DEN 25 grantje01
58 Josh Huestis PF 2018-02-01 * OKC DEN 12 huestjo01
86 Patrick Patterson PF 2018-02-01 * OKC DEN 15 pattepa01
106 Russell Westbrook PG 2018-02-01 * OKC DEN 39 westbru01
Game.ID
1 2018-02-01 * OKC
2 2018-02-01 * OKC
8 2018-02-01 * OKC
34 2018-02-01 * OKC
35 2018-02-01 * OKC
41 2018-02-01 * OKC
46 2018-02-01 * OKC
58 2018-02-01 * OKC
86 2018-02-01 * OKC
106 2018-02-01 * OKC
> zz <- merge(yy, xx, all.x = TRUE)
> zz
MP Name Pos Date Tm Opp Player.ID
1 4 Terrance Ferguson SG 2018-02-01 * OKC DEN fergute01
2 12 Josh Huestis PF 2018-02-01 * OKC DEN huestjo01
3 14 Raymond Felton PG 2018-02-01 * OKC DEN feltora01
4 15 Patrick Patterson PF 2018-02-01 * OKC DEN pattepa01
5 25 Jerami Grant PF 2018-02-01 * OKC DEN grantje01
6 29 Alex Abrines SG 2018-02-01 * OKC DEN abrinal01
7 32 Steven Adams C 2018-02-01 * OKC DEN adamsst01
8 34 Carmelo Anthony PF 2018-02-01 * OKC DEN anthoca01
9 36 Paul George PF 2018-02-01 * OKC DEN georgpa01
10 39 Russell Westbrook PG 2018-02-01 * OKC DEN westbru01
Game.ID Depth.Chart
1 2018-02-01 * OKC 10
2 2018-02-01 * OKC 9
3 2018-02-01 * OKC 8
4 2018-02-01 * OKC 7
5 2018-02-01 * OKC 6
6 2018-02-01 * OKC 5
7 2018-02-01 * OKC 4
8 2018-02-01 * OKC 3
9 2018-02-01 * OKC 2
10 2018-02-01 * OKC 1
Finally, I need to extract the value of zz$Depth.Chart that corresponds to the original observation, 5.
> zz[zz$MP == 29, c('Depth.Chart')]
[1] 5
Levels: 1 10 2 3 4 5 6 7 8 9
I would like to define a function that executes the laborious and messy steps above for each observation in a data frame and returns a vector of the results. How can I reference the value of df$MP that corresponds to the observation I'm working on without explicitly calling it 29, like I do above? Here are a few of the thing I've tried, unsuccessfully.
> f1 <- function(col1, df, col2){
+ lapply(col1, function(i){
+ df2 <- df[col1 == i, col2]
+ df3 <- data.frame(cbind(sort(df2, decreasing = TRUE), rownames(data.table(sort(df2, decreasing = TRUE)))))
+ df3[i, 2]
+ })}
> f1(df$Game.ID, df, c('MP'))[1:10]
[[1]]
[1] 7
Levels: 1 10 2 3 4 5 6 7 8 9
[[2]]
[1] 7
Levels: 1 10 2 3 4 5 6 7 8 9
[[3]]
[1] 6
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[4]]
[1] 8
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[5]]
[1] 8
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[6]]
[1] 3
Levels: 1 2 3 4 5 6 7 8
[[7]]
[1] 5
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[8]]
[1] 7
Levels: 1 10 2 3 4 5 6 7 8 9
[[9]]
[1] 9
Levels: 1 10 11 2 3 4 5 6 7 8 9
[[10]]
[1] 1
Levels: 1 10 2 3 4 5 6 7 8 9
> f1 <- function(col1, df, col2){
+ lapply(col1, function(i){
+ df2 <- df[col1 == i, col2]
+ df3 <- data.frame(cbind(sort(df2, decreasing = TRUE), rownames(data.table(sort(df2, decreasing = TRUE)))))
+ df3[df3$X1 == i, 2]
+ })}
> f1(df$Game.ID, df, c('MP'))
Hide Traceback
Rerun with Debug
Error in Ops.factor(df3$X1, i) : level sets of factors are different
7.
stop("level sets of factors are different")
6.
Ops.factor(df3$X1, i)
5.
`[.data.frame`(df3, df3$X1 == i, 2)
4.
df3[df3$X1 == i, 2]
3.
FUN(X[[i]], ...)
2.
lapply(col1, function(i) {
df2 <- df[col1 == i, col2]
df3 <- data.frame(cbind(sort(df2, decreasing = TRUE), rownames(data.table(sort(df2,
decreasing = TRUE))))) ...
1.
f1(df$Game.ID, df, c("MP"))
> f1 <- function(col1, df, col2){
+ lapply(col1, function(i){
+ df2 <- df[col1 == i, col2]
+ df3 <- data.frame(cbind(sort(df2, decreasing = TRUE), rownames(data.table(sort(df2, decreasing = TRUE)))))
+ df3[col2 == i, 2]
+ })}
> f1(df$Game.ID, df, c('MP'))[1:10]
[[1]]
factor(0)
Levels: 1 10 2 3 4 5 6 7 8 9
[[2]]
factor(0)
Levels: 1 10 2 3 4 5 6 7 8 9
[[3]]
factor(0)
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[4]]
factor(0)
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[5]]
factor(0)
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[6]]
factor(0)
Levels: 1 2 3 4 5 6 7 8
[[7]]
factor(0)
Levels: 1 10 11 12 13 2 3 4 5 6 7 8 9
[[8]]
factor(0)
Levels: 1 10 2 3 4 5 6 7 8 9
[[9]]
factor(0)
Levels: 1 10 11 2 3 4 5 6 7 8 9
[[10]]
factor(0)
Levels: 1 10 2 3 4 5 6 7 8 9
I guess I don't fully understand how R treats this i variable inside the function; or, therefore, how reference it appropriately. In looking through this forum, I found generic examples of nesting functions inside of functions in Python but not in R. Any help would be much appreciated.
EDIT
Here is a simpler subset of my data:
> dput(df)
structure(list(MP = c(29L, 32L, 3L, 34L, 14L, 3L, 40L, 17L, 13L,
14L, 4L, 36L, 6L, 33L, 25L, 12L, 17L, 3L, 15L, 28L, 33L, 39L,
30L), Player.ID = structure(c(1L, 2L, 3L, 8L, 14L, 16L, 21L,
26L, 30L, 34L, 35L, 42L, 41L, 43L, 46L, 58L, 62L, 79L, 86L, 100L,
102L, 106L, 107L), .Label = c("abrinal01", "adamsst01", "aldrico01",
"aldrila01", "anderky01", "anderry01", "antetgi01", "anthoca01",
"anunoog01", "arthuda01", "bartowi01", "bealbr01", "bertada01",
"bjeline01", "brogdma01", "brookaa01", "brookdi01", "brownlo01",
"brownst02", "bullore01", "butleji01", "buyckdw01", "capelca01",
"chandwi01", "craigto01", "crawfja01", "davisde01", "dellama01",
"derozde01", "dienggo01", "drumman01", "ennisja01", "farieke01",
"feltora01", "fergute01", "forbebr01", "fraziti01", "gallola01",
"gasolma01", "gasolpa01", "georgma01", "georgpa01", "gibsota01",
"ginobma01", "gortama01", "grantje01", "greenda02", "greenge01",
"greenja01", "griffbl01", "hardeja01", "harrian01", "harriga01",
"henrymy01", "hensojo01", "hilarne01", "hillida01", "huestjo01",
"ibakase01", "johnsst04", "jokicni01", "jonesty01", "kennalu01",
"kilpase01", "lauvejo01", "lowryky01", "lylestr01", "mahinia01",
"makerth01", "martija01", "mbahalu01", "mclembe01", "meeksjo01",
"middlkh01", "millspa02", "moreler01", "morrima02", "mudiaem01",
"muhamsh01", "munfoxa02", "murrade01", "murraja01", "noguelu01",
"oubreke01", "parketo01", "pattepa01", "paulbr01", "paulch01",
"plumlma02", "poeltja01", "porteot01", "powelno01", "reedwi02",
"satorto01", "scottmi01", "seldewa01", "siakapa01", "smithis01",
"snellto01", "teaguje01", "tollian01", "townska01", "tuckepj01",
"valanjo01", "vaughra01", "westbru01", "wiggian01", "wilsodj01",
"wrighde01"), class = "factor"), Game.ID = structure(c(7L, 7L,
6L, 7L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 6L, 6L, 7L, 7L, 6L, 6L,
7L, 6L, 6L, 7L, 6L), .Label = c("2018-02-01 * DEN", "2018-02-01 * DET",
"2018-02-01 * HOU", "2018-02-01 * MEM", "2018-02-01 * MIL", "2018-02-01 * MIN",
"2018-02-01 * OKC", "2018-02-01 * SAS", "2018-02-01 * TOR", "2018-02-01 * WAS"
), class = "factor")), .Names = c("MP", "Player.ID", "Game.ID"
), row.names = c(1L, 2L, 3L, 8L, 14L, 16L, 21L, 26L, 30L, 34L,
35L, 41L, 42L, 43L, 46L, 58L, 62L, 79L, 86L, 100L, 102L, 106L,
107L), class = "data.frame")
You're using data.table for little steps in your process, but you should just use it for the whole thing. It's very convenient for doing operations "by group", in this case using rank() by Game.ID. Using your small sample data:
library(data.table)
setDT(df)
df[, Depth.Chart := rank(-MP), by = Game.ID]
df
# MP Player.ID Game.ID Depth.Chart
# 1: 29 abrinal01 2018-02-01 * OKC 5.0
# 2: 32 adamsst01 2018-02-01 * OKC 4.0
# 3: 3 aldrico01 2018-02-01 * MIN 12.0
# 4: 34 anthoca01 2018-02-01 * OKC 3.0
# 5: 14 bjeline01 2018-02-01 * MIN 8.0
# 6: 3 brookaa01 2018-02-01 * MIN 12.0
# 7: 40 butleji01 2018-02-01 * MIN 1.0
# 8: 17 crawfja01 2018-02-01 * MIN 6.5
# 9: 13 dienggo01 2018-02-01 * MIN 9.0
# 10: 14 feltora01 2018-02-01 * OKC 8.0
# 11: 4 fergute01 2018-02-01 * OKC 10.0
# 12: 36 georgpa01 2018-02-01 * OKC 2.0
# 13: 6 georgma01 2018-02-01 * MIN 10.0
# 14: 33 gibsota01 2018-02-01 * MIN 2.5
# 15: 25 grantje01 2018-02-01 * OKC 6.0
# 16: 12 huestjo01 2018-02-01 * OKC 9.0
# 17: 17 jonesty01 2018-02-01 * MIN 6.5
# 18: 3 muhamsh01 2018-02-01 * MIN 12.0
# 19: 15 pattepa01 2018-02-01 * OKC 7.0
# 20: 28 teaguje01 2018-02-01 * MIN 5.0
# 21: 33 townska01 2018-02-01 * MIN 2.5
# 22: 39 westbru01 2018-02-01 * OKC 1.0
# 23: 30 wiggian01 2018-02-01 * MIN 4.0
# MP Player.ID Game.ID Depth.Chart
rank, by default, averages ties, but see ?rank for other options.
Related
R ggplot2 - How to plot 2 boxplots on the same x value
suppose I have two boxplots. trial1 <- ggplot(completionTime, aes(fill=Condition, x=Scenario, y=Trial1)) trial1 + geom_boxplot()+geom_point(position=position_dodge(width=0.75)) + ylim(0, 160) trial2 <- ggplot(completionTime, aes(fill=Condition, x=Scenario, y=Trial2)) trial2 + geom_boxplot()+geom_point(position=position_dodge(width=0.75)) + ylim(0, 160) How can I plot trial 1 and trial 2 on the same plot and same respective X? they have the same range of y. I looked at geom_boxplot(position="identity"), but that plots the two conditions(fill) on the same X. I want to plot two y column on the same X. Edit: the dataset User Condition Scenario Trial1 Trial2 1 1 ME a 67 41 2 1 ME b 70 42 3 1 ME c 40 15 4 1 ME d 65 23 5 1 ME e 45 45 6 1 SE a 100 34 7 1 SE b 54 23 8 1 SE c 70 23 9 1 SE d 56 15 10 1 SE e 30 20 11 2 ME a 42 23 12 2 ME b 22 12 13 2 ME c 28 8 14 2 ME d 22 8 15 2 ME e 38 37 16 2 SE a 59 18 17 2 SE b 65 14 18 2 SE c 75 7 19 2 SE d 37 9 20 2 SE e 31 7 dput() structure(list(User = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Condition = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("ME", "SE"), class = "factor"), Scenario = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("a", "b", "c", "d", "e"), class = "factor"), Trial1 = c(67L, 70L, 40L, 65L, 45L, 100L, 54L, 70L, 56L, 30L, 42L, 22L, 28L, 22L, 38L, 59L, 65L, 75L, 37L, 31L), Trial2 = c(41L, 42L, 15L, 23L, 45L, 34L, 23L, 23L, 15L, 20L, 23L, 12L, 8L, 8L, 37L, 18L, 14L, 7L, 9L, 7L)), .Names = c("User", "Condition", "Scenario", "Trial1", "Trial2"), class = "data.frame", row.names = c(NA, -20L))
You could try using interaction to combine two of your factors and plot against a third. For example, assuming you want to fill by condition as in your original code: library(tidyr) completionTime %>% gather(trial, value, -Scenario, -Condition, -User) %>% ggplot(aes(interaction(Scenario, trial), value)) + geom_boxplot(aes(fill = Condition)) Result:
Over Function in R gives unexpected results
I want to perform an over from the sp package. However, the results I am getting are unexpected. My polygon overlays with nine Points. However, after the over() it only returns 1 point. Why is that? Here a reproducible example! library(sp) # The point data myPoi <- structure(list(index = c(2L, 17L, 19L, 23L, 34L, 71L, 82L, 85L, 92L, 96L, 102L, 7L, 20L, 29L, 40L, 50L, 100L, 103L, 84L, 91L, 11L, 61L, 70L, 76L, 45L, 1L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 13L, 14L, 18L, 21L, 22L, 24L, 25L, 26L, 27L, 28L, 30L, 31L, 33L, 35L, 36L, 37L, 38L, 41L, 42L, 43L, 44L, 47L, 48L, 49L, 51L, 52L, 55L, 56L, 58L, 60L, 62L, 63L, 64L, 68L, 69L, 72L, 73L, 74L, 75L, 77L, 78L, 79L, 80L, 81L, 83L, 87L, 88L, 89L, 90L, 93L, 94L, 95L, 97L, 98L, 99L, 101L, 104L, 12L, 15L, 66L, 86L), x = c(13.0165026, 13.0164516, 13.0164489, 13.0164114, 13.0164328, 13.0162478, 13.016237, 13.0164516, 13.0160949, 13.0163309, 13.0164597, 13.02528, 13.0159466, 13.0437784, 13.0309044, 13.0335449, 13.0289418, 13.0253004, 13.0446292, 13.0434527, 13.0394242, 12.9720291, 13.0315502, 13.0397486, 13.0195456, 13.036048, 13.0356159, 13.0283043, 13.0178174, 13.0395106, 13.0182921, 13.0286984, 13.0382137, 13.0389044, 13.035627, 13.0179864, 13.0387113, 13.0413548, 13.0176216, 13.0395562, 13.0330164, 13.0377135, 13.0410115, 13.0176398, 13.0183699, 13.0413454, 13.0178389, 13.0373272, 13.0383505, 13.0331394, 13.0181419, 13.0184316, 13.0415063, 13.0373487, 13.0390412, 13.0179944, 13.0298012, 13.0358995, 13.041501, 13.0331575, 13.0183914, 13.0293197, 13.0389486, 13.0176076, 13.0357577, 13.017694, 13.0411228, 13.0184692, 13.0182787, 13.0184531, 13.0348261, 13.0387716, 13.0377537, 13.0358131, 13.0332953, 13.0385517, 13.0409887, 13.0360657, 13.0181661, 13.0411724, 13.0359306, 13.0382338, 13.0383103, 13.0348797, 13.0357777, 13.043133, 13.0390318, 13.0384913, 13.0412797, 13.0359298, 13.0386354, 13.0297737, 13.0294306, 13.033415), y = c(52.4009649, 52.4003774, 52.4004641, 52.40168, 52.4014313, 52.4022691, 52.4024115, 52.4012316, 52.4024066, 52.4002121, 52.4002186, 52.4011368, 52.4063829, 52.4004306, 52.408314, 52.4033442, 52.3988618, 52.4036956, 52.403118, 52.4023966, 52.4001135, 52.4045952, 52.4124868, 52.4040269, 52.4044583, 52.4021688, 52.4021458, 52.4025064, 52.4020523, 52.4024279, 52.401748, 52.4032264, 52.4022585, 52.4023951, 52.4020268, 52.400694, 52.4024549, 52.4021521, 52.4021309, 52.4020433, 52.4025458, 52.4023542, 52.4022225, 52.3996966, 52.4010574, 52.402468, 52.4005992, 52.4022168, 52.4020114, 52.4024885, 52.4007988, 52.4011998, 52.402247, 52.4020875, 52.4021521, 52.4019705, 52.4048047, 52.402235, 52.4023853, 52.4026649, 52.4016137, 52.4048988, 52.4020327, 52.3999584, 52.402231, 52.4005386, 52.4024549, 52.4013388, 52.400928, 52.4014714, 52.4013764, 52.4019623, 52.4019729, 52.4017299, 52.402618, 52.4019558, 52.4023567, 52.4020376, 52.4018642, 52.4021448, 52.4019606, 52.4021251, 52.4023698, 52.401446, 52.4019525, 52.4011382, 52.402283, 52.4024483, 52.4020605, 52.4017324, 52.4101571, 52.405268, 52.4046133, 52.4043887), poi = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L), .Label = c("artwork", "attraction", "gate", "memorial", "ruins", "statue", "viewpoint"), class = "factor"), gewichtung = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L)), .Names = c("index", "x", "y", "poi", "gewichtung"), class = "data.frame", row.names = c(NA, -94L)) # corner of polygon cornersPoly <- structure(c(13.0302143096924, 13.0232620239258, 13.0237770080566, 13.0296993255615, 13.0326175689697, 52.4066068887721, 52.4047742038877, 52.400375449523, 52.3982282791922, 52.4042505656507), .Dim = c(5L, 2L)) And here my code! # make points spatial coordinates(myPoi) <- ~x + y proj4string(myPoi) <- CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=#null +wktext +no_defs") # make polygon from matrix # create Polygon myPolygon <- Polygon(cornersPoly) # create Polygon List myPolygonList <- Polygons(list(myPolygon), 1) # create spatial Polygon myPolygonSpatial <- SpatialPolygons(list(myPolygonList)) # assign CRS proj4string(myPolygonSpatial) <- CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=#null +wktext +no_defs") # perform the over test <- over(myPolygonSpatial, myPoi) edit with the comment from Josh O'Brien I get that as a result 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 NA NA NA NA NA NA NA NA NA NA NA 1 NA NA NA NA 1 1 NA NA NA NA NA NA NA NA NA 1 NA NA NA 1 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 1 NA NA NA NA 1 NA NA 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 1 1 NA
Try test <- over(myPolygonSpatial, myPoi, returnList = TRUE) and look at the first element of the list returned. The issue why this looks complicated is that myPolygonSpatial may contain multiple points; the list will contain the set of sets of points. Reading vignette(over) may also pay off.
Sampling distribution and sum of tables
I've made a few experiments and each experiment led to the apparition of color. As I can't do more experiments, I want to sample by size=30 and see what frequency table (of colors) I could obtain for 1000 sampling. The resulting frequency table should be the sum of the 1000 frequency table. I think about concatenating table as follows and try to agregate, but it did not work: mydata=structure(list(Date = structure(c(11L, 1L, 9L, 9L, 10L, 1L, 2L, 3L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 7L, 4L, 4L, 4L, 6L, 6L, 11L, 5L, 4L, 7L, 10L, 6L, 6L, 2L, 5L, 7L, 11L, 1L, 9L, 11L, 11L, 11L, 1L, 1L), .Label = c("01/02/2016", "02/02/2016", "03/02/2016", "08/02/2016", "10/02/2016", "11/02/2016", "16/02/2016", "22/02/2016", "26/01/2016", "27/01/2016", "28/01/2016"), class = "factor"), Color = structure(c(30L, 33L, 11L, 1L, 18L, 18L, 11L, 16L, 19L, 19L, 22L, 1L, 18L, 18L, 13L, 14L, 13L, 18L, 24L, 24L, 11L, 24L, 2L, 33L, 25L, 1L, 30L, 5L, 24L, 18L, 13L, 35L, 19L, 19L, 18L, 23L, 19L, 8L, 19L, 14L), .Label = c("ARD", "ARP", "BBB", "BIE", "CFX", "CHR", "DDD", "DOO", "EAU", "ELY", "EPI", "ETR", "GEN", "GER", "GGG", "GIS", "ISE", "JUV", "LER", "LES", "LON", "LYR", "MON", "NER", "NGY", "NOJ", "NYO", "ORI", "PEO", "RAY", "RRR", "RSI", "SEI", "SEP", "VIL", "XQU", "YYY", "ZYZ"), class = "factor"), Categorie = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "1,2", "1,2,3", "1,3", "2", "2,3", "3", "4", "5"), class = "factor"), Portion_Longueur = c(3L, 4L, 1L, 1L, 2L, 4L, 5L, 6L, 7L, 7L, 8L, 8L, 9L, 8L, 8L, 9L, 11L, 7L, 7L, 7L, 9L, 8L, 3L, 8L, 7L, 11L, 2L, 9L, 8L, 5L, 8L, 12L, 3L, 4L, 1L, 3L, 3L, 3L, 4L, 5L)), .Names = c("Date", "Color", "Categorie", "Portion_Longueur"), row.names = c(NA, 40L), class = "data.frame") for (i in 1:1000) { mysamp= sample(mydata$Color,size=30) x=data.frame(table(mysamp)) if (i==1) w=x else w <- c(w, x) } aggregate(w$Freq, by=list(Color=w$mysamp), FUN=sum) Example, for 3 sampling, for (i in 1:3) I expect have sum as follow : But I do not have Sum, instead I have: Color x 1 ARD 2 2 ARP 1 3 BBB 0 4 BIE 0 5 CFX 0 6 CHR 0 7 DDD 0 8 DOO 1 9 EAU 0 10 ELY 0 11 EPI 3 12 ETR 0 13 GEN 2 14 GER 2 15 GGG 0 16 GIS 1 17 ISE 0 18 JUV 4 19 LER 5 20 LES 0 21 LON 0 22 LYR 1 23 MON 1 24 NER 2 25 NGY 1 26 NOJ 0 27 NYO 0 28 ORI 0 29 PEO 0 30 RAY 1 31 RRR 0 32 RSI 0 33 SEI 2 34 SEP 0 35 VIL 1 36 XQU 0 37 YYY 0 38 ZYZ 0 How to do this ? Thanks a lot
Your for loop is what's causing your issues. You end up creating a big list that is somewhat difficult to perform calculations on (check out names(w) to see what I mean). A better data structure would allow for easier calculations: x = NULL #initialize for (i in 1:1000) { mysamp = sample(mydata$Color,size=30) #sample mysamp = data.frame(table(mysamp)) #frequency x = rbind(x, mysamp) #bind to x } aggregate(Freq~mysamp, data = x, FUN = sum) #perform calculation Note that this loop runs a bit slower than your loop. This is because of the rbind() function. See this post. Maybe someone will come along with a more efficient solution.
Conditionally remove a row based on another id code
In a dataset which contains many ids, I am only trying to manipulate rows which have id 7 or 9, and leave everything else untouched. I am trying to conditionally remove a row from 7 or 9 in all instances where there isn't a variable that corresponds to it. So, if in the case of the dput example below, I want to remove the ninth row from id=9 because id=7 does not have an itemcode=2. Vice versa for id=7, I am trying to remove its itemcode=9 because id=9 does not have it. id client item itemcode unit X2001 X2002 X2003 X2004 X2005 X2006 X2007 ... 7 7 Bob eighth 8 100 13 18 15 NA NA NA NA 8 7 Bob ninth 9 100 11 21 10 NA NA NA NA 9 9 Bob_new first 1 100 NA NA NA 23 18 25 18 Code: structure(list(id = c(7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L), client = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("Bob", "Bob_new", "Mark"), class = "factor"), item = structure(c(3L, 9L, 4L, 2L, 8L, 7L, 1L, 5L, 3L, 6L, 9L, 4L, 2L, 8L, 7L, 1L, 3L ), .Label = c("eighth", "fifth", "first", "fourth", "ninth", "second", "seventh", "sixth", "third"), class = "factor"), itemcode = c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L ), unit = c(100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L), X2001 = structure(c(5L, 6L, 1L, 4L, 2L, 5L, 3L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L ), .Label = c("11", "12", "13", "22", "24", "25", "NA"), class = "factor"), X2002 = structure(c(4L, 8L, 1L, 3L, 7L, 2L, 5L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("13", "14", "15", "17", "18", "21", "22", "24", "NA"), class = "factor"), X2003 = structure(c(5L, 1L, 4L, 2L, 6L, 1L, 3L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("10", "11", "15", "19", "23", "24", "NA"), class = "factor"), X2004 = structure(c(7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 5L, 4L, 2L, 6L, 1L, 3L, 4L, 3L, 4L), .Label = c("11", "14", "15", "20", "23", "25", "NA"), class = "factor"), X2005 = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L, 2L, 4L, 3L, 5L, 3L, 1L, 4L, 3L), .Label = c("11", "13", "18", "19", "25", "NA"), class = "factor"), X2006 = structure(c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 6L, 1L, 2L, 5L, 3L, 7L, 8L, 4L), .Label = c("10", "15", "18", "19", "20", "22", "23", "25", "NA"), class = "factor"), X2007 = structure(c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 4L, 7L, 6L, 2L, 4L, 1L, 5L, 5L, 3L), .Label = c("12", "13", "16", "18", "19", "21", "24", "NA"), class = "factor")), .Names = c("id", "client", "item", "itemcode", "unit", "X2001", "X2002", "X2003", "X2004", "X2005", "X2006", "X2007"), class = "data.frame", row.names = c(NA, -17L )) ———————————————————————————————————————— ANOTHER SCENARIO: before: structure(list(id = c(7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L), client = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("Bob", "Bob_new", "Mark"), class = "factor"), item = structure(c(3L, 9L, 10L, 9L, 4L, 2L, 8L, 7L, 7L, 1L, 5L, 3L, 6L, 9L, 4L, 2L, 8L, 7L, 1L, 3L), .Label = c("eighth", "fifth", "first", "fourth", "ninth", "second", "seventh", "sixth", "third", "third "), class = "factor"), itemcode = c(1L, 3L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L), type = structure(c(1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B", "C"), class = "factor"), unit = c(100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L), X2001 = c(24L, 25L, 30L, 26L, 11L, 22L, 12L, 25L, 24L, 13L, 11L, NA, NA, NA, NA, NA, NA, NA, NA, NA), X2002 = c(17L, 24L, 12L, 96L, 13L, 15L, 22L, 21L, 14L, 18L, 21L, NA, NA, NA, NA, NA, NA, NA, NA, NA), X2003 = c(23L, 10L, 46L, 94L, 19L, 11L, 24L, 19L, 10L, 15L, 10L, NA, NA, NA, NA, NA, NA, NA, NA, NA), X2004 = c(NA, NA, 43L, 83L, NA, NA, NA, 6L, NA, NA, NA, 23L, 20L, 14L, 25L, 11L, 15L, 20L, 15L, 20L), X2005 = c(NA, NA, 97L, 86L, NA, NA, NA, 17L, NA, NA, NA, 18L, 13L, 19L, 18L, 25L, 18L, 11L, 19L, 18L), X2006 = c(NA, NA, 11L, 91L, NA, NA, NA, 11L, NA, NA, NA, 25L, 22L, 10L, 15L, 20L, 18L, 23L, 25L, 19L), X2007 = c(NA, NA, 19L, 27L, NA, NA, NA, 15L, NA, NA, NA, 18L, 24L, 21L, 13L, 18L, 12L, 19L, 19L, 16L)), .Names = c("id", "client", "item", "itemcode", "type", "unit", "X2001", "X2002", "X2003", "X2004", "X2005", "X2006", "X2007"), class = "data.frame", row.names = c(NA, -20L)) after: structure(list(id = c(7L, 7L, 7L, 7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 10L), client = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("Bob", "Bob_new", "Mark"), class = "factor"), item = structure(c(2L, 7L, 3L, 1L, 5L, 4L, 2L, 6L, 3L, 1L, 5L, 4L, 2L), .Label = c("fifth", "first", "fourth", "seventh", "sixth", "third", "third "), class = "factor"), itemcode = c(1L, 3L, 4L, 5L, 6L, 7L, 1L, 3L, 4L, 5L, 6L, 7L, 1L), type = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor"), unit = c(100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L), X2001 = c(24L, 10L, 11L, 22L, 12L, 17L, NA, NA, NA, NA, NA, NA, NA), X2002 = c(17L, 87L, 13L, 15L, 22L, 19L, NA, NA, NA, NA, NA, NA, NA), X2003 = c(23L, 47L, 19L, 11L, 24L, 17L, NA, NA, NA, NA, NA, NA, NA), X2004 = c(NA, 28L, NA, NA, NA, 28L, 23L, 14L, 25L, 11L, 15L, 20L, 20L), X2005 = c(NA, 43L, NA, NA, NA, 16L, 18L, 19L, 18L, 25L, 18L, 11L, 18L), X2006 = c(NA, 69L, NA, NA, NA, 5L, 25L, 10L, 15L, 20L, 18L, 23L, 19L), X2007 = c(NA, 72L, NA, NA, NA, 20L, 18L, 21L, 13L, 18L, 12L, 19L, 16L)), .Names = c("id", "client", "item", "itemcode", "type", "unit", "X2001", "X2002", "X2003", "X2004", "X2005", "X2006", "X2007"), class = "data.frame", row.names = c(NA, -13L)) I could implement the said filter code to remove items which do not exist in its corresponding place (id 7 and 9). But if there are sub levels for items, like type of item. I am also trying to remove items if they don't have a type similar in the corresponding field.
You could use filter from dplyr library(dplyr) filter(df_all, itemcode %in% intersect(itemcode[id==7], itemcode[id==9])|!id %in% c(7,9) ) # id client item itemcode unit X2001 X2002 X2003 X2004 X2005 X2006 X2007 #1 7 Bob first 1 100 24 17 23 NA NA NA NA #2 7 Bob third 3 100 25 24 10 NA NA NA NA #3 7 Bob fourth 4 100 11 13 19 NA NA NA NA #4 7 Bob fifth 5 100 22 15 11 NA NA NA NA #5 7 Bob sixth 6 100 12 22 24 NA NA NA NA #6 7 Bob seventh 7 100 24 14 10 NA NA NA NA #7 7 Bob eighth 8 100 13 18 15 NA NA NA NA #8 9 Bob_new first 1 100 NA NA NA 23 18 25 18 #9 9 Bob_new third 3 100 NA NA NA 14 19 10 21 #10 9 Bob_new fourth 4 100 NA NA NA 25 18 15 13 #11 9 Bob_new fifth 5 100 NA NA NA 11 25 20 18 #12 9 Bob_new sixth 6 100 NA NA NA 15 18 18 12 #13 9 Bob_new seventh 7 100 NA NA NA 20 11 23 19 #14 9 Bob_new eighth 8 100 NA NA NA 15 19 25 19 #15 10 Mark first 1 100 NA NA NA 20 18 19 16 Update Based on the new dataset, perhaps this helps library(dplyr) library(tidyr) dfnew %>% unite(itemtype, itemcode,type) %>% filter(itemtype %in% intersect(itemtype[id==7], itemtype[id==9])|!id %in% c(7,9)) %>% separate(itemtype, c('itemcode', 'type')) # id client item itemcode type unit X2001 X2002 X2003 X2004 X2005 X2006 # 1 7 Bob first 1 A 100 24 17 23 NA NA NA # 2 7 Bob third 3 B 100 30 12 46 43 97 11 # 3 7 Bob fourth 4 A 100 11 13 19 NA NA NA # 4 7 Bob fifth 5 A 100 22 15 11 NA NA NA # 5 7 Bob sixth 6 A 100 12 22 24 NA NA NA # 6 7 Bob seventh 7 A 100 25 21 19 6 17 11 # 7 9 Bob_new first 1 A 100 NA NA NA 23 18 25 # 8 9 Bob_new third 3 B 100 NA NA NA 14 19 10 # 9 9 Bob_new fourth 4 A 100 NA NA NA 25 18 15 # 10 9 Bob_new fifth 5 A 100 NA NA NA 11 25 20 # 11 9 Bob_new sixth 6 A 100 NA NA NA 15 18 18 # 12 9 Bob_new seventh 7 A 100 NA NA NA 20 11 23 # 13 10 Mark first 1 A 100 NA NA NA 20 18 19 # X2007 #1 NA #2 19 #3 NA #4 NA #5 NA #6 15 #7 18 #8 21 #9 13 #10 18 #11 12 #12 19 #13 16
If I understand the problem: every itemcode in id=9 subset must have identical itemcode in id=7 subset (and reverse). If it is not the case then we filter the row with the non-pair itemcode out, but leave everything with id not in 7 or 9. Here is one way of doing it: First get common item codes: items_9 <- df_all$itemcode[ df_all$id==9 ] items_7 <- df_all$itemcode[ df_all$id==7 ] items_common <- items_9[ items_9 %in% items_7 ] select everything with common itemcodes for 7 and 9 and the rest: df_new <- df_all[ which( ( df_all$id %in% c(7, 9) & df_all$itemcode %in% items_common ) | !df_all$id %in% c(7,9) ) ,]
library(dplyr) df$remove <- paste(df$itemcode, df$type) df<-invisible(filter(df, remove %in% intersect(remove[type==7], remove[type==9])|!type %in% c(7,9) )) #Remove the additional column after filter df$remove <- NULL
You could do something like this, which runs setdiff in both directions. The cl() function wasn't really necessary, but I really don't like writing the same expression over and over again. f <- function(x, y) setdiff(union(x, y), x) cl <- function(var) substitute(df$itemcode[df$id == x], list(x = var)) So now you can call f() on c(id7, id9) and then reverse it and get the c(id9, id7) result. do.call(f, x <- list(cl(7), cl(9))) # [1] 2 do.call(f, rev(x)) # [1] 9
Use dplyr to find genotype frequency across SNPs
To find genotype frequency across SNPs I need to find the proportion of a certain genotype (XX, YX, or YY) in the total number of samples (XX, YX, and YY). I think I would need to start my dplyr statement with dat %>% group_by(Assay) %>% but I don't know how to finish it. The data, dat, provided below and dput at the bottom. Source: local data frame [143 x 3] Groups: Assay Assay Final n 1 One_apoe-83 Invalid 2 2 One_apoe-83 No Call 9 3 One_apoe-83 NTC 2 4 One_apoe-83 XX 4 5 One_apoe-83 YX 41 6 One_apoe-83 YY 134 7 One_CD9-269 Invalid 2 8 One_CD9-269 No Call 5 9 One_CD9-269 NTC 2 10 One_CD9-269 XX 99 .. ... ... ... I could use a for loop across SNPs to get what I'm looking for with boolean patterning for each genotype but that would be very verbose. for(i in seq(levels(dat$Assay))) { storage_df[i,1] <- dat[dat$Assay == levels(dat$Assay)[i],]$XX / (dat[dat$Assay == levels(dat$Assay)[i],]$XX + dat[dat$Assay == levels(dat$Assay)[i],]$YX + dat[dat$Assay == levels(dat$Assay)[i],]$XY) ... You get the point. How would I do this in dplyr? The whole object is below. dat <- structure(list(Assay = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L), .Label = c("One_apoe-83", "One_CD9-269", "One_Cytb_26", "One_E2", "One_ghsR-66", "One_IL8r-362", "One_KPNA-422", "One_lpp1-44", "One_MHC2_190", "One_MHC2_251", "One_Prl2", "One_redd1-414", "One_STC-410", "One_STR07", "One_sys1-230", "One_U1004-183", "One_U1105", "One_U1201-492", "One_U1203-175", "One_U1209-111", "One_U1212-106", "One_U401-224", "One_vamp5-255", "One_ZNF-61"), class = "factor"), Final = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("Invalid", "No Call", "NTC", "XX", "YX", "YY"), class = "factor"), n = c(2L, 9L, 2L, 4L, 41L, 134L, 2L, 5L, 2L, 99L, 75L, 9L, 2L, 7L, 2L, 110L, 71L, 2L, 8L, 2L, 110L, 59L, 11L, 2L, 6L, 2L, 67L, 86L, 29L, 2L, 3L, 2L, 152L, 28L, 5L, 2L, 4L, 2L, 78L, 81L, 25L, 2L, 4L, 2L, 115L, 62L, 7L, 2L, 17L, 2L, 80L, 62L, 29L, 2L, 13L, 2L, 59L, 68L, 48L, 2L, 7L, 2L, 48L, 86L, 47L, 2L, 7L, 2L, 42L, 87L, 52L, 2L, 3L, 2L, 47L, 81L, 57L, 2L, 9L, 2L, 40L, 85L, 54L, 2L, 8L, 2L, 52L, 86L, 42L, 2L, 7L, 2L, 9L, 39L, 133L, 2L, 8L, 2L, 101L, 71L, 8L, 2L, 13L, 2L, 20L, 82L, 73L, 2L, 11L, 2L, 27L, 75L, 75L, 2L, 6L, 2L, 3L, 40L, 139L, 2L, 13L, 2L, 59L, 82L, 34L, 2L, 19L, 2L, 20L, 84L, 65L, 2L, 11L, 2L, 119L, 47L, 11L, 2L, 8L, 2L, 51L, 100L, 29L)), class = "data.frame", .Names = c("Assay", "Final", "n"), row.names = c(NA, -143L))
Hope I am not misunderstanding. Are you looking for below: Assume the data structure is: df <- structure(list(Assay = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("One_apoe-83", "One_CD9-269"), class = "factor"), Final = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L ), .Label = c("Invalid", "No Call", "NTC", "XX", "YX", "YY" ), class = "factor"), n = c(2L, 9L, 2L, 4L, 41L, 134L, 2L, 5L, 2L, 99L)), .Names = c("Assay", "Final", "n"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")) Code df %>% group_by(Assay) %>% mutate(n_percent = n/sum(n)*100) # Assay Final n n_percent # 1 One_apoe-83 Invalid 2 1.041667 # 2 One_apoe-83 No Call 9 4.687500 # 3 One_apoe-83 NTC 2 1.041667 # 4 One_apoe-83 XX 4 2.083333 # 5 One_apoe-83 YX 41 21.354167 # 6 One_apoe-83 YY 134 69.791667 # 7 One_CD9-269 Invalid 2 1.851852 # 8 One_CD9-269 No Call 5 4.629630 # 9 One_CD9-269 NTC 2 1.851852 # 10 One_CD9-269 XX 99 91.666667 Option 2 Here is the code based on the comment. A line is added to filter out the elements you don't want. df %>% filter(! Final %in% c("Invalid", "No Call", "NTC")) %>% group_by(Assay) %>% mutate(n_percent = n/sum(n)*100) # Source: local data frame [4 x 4] # Groups: Assay # # Assay Final n n_percent # 1 One_apoe-83 XX 4 2.234637 # 2 One_apoe-83 YX 41 22.905028 # 3 One_apoe-83 YY 134 74.860335 # 4 One_CD9-269 XX 99 100.000000