0

I am trying to extract a specific column from a specific row on my excel sheet (df). However, when I try to do so I get the message:

Error: ... must evaluate to column positions or names, not a list

Call `rlang::last_error()` to see a backtrace.

When I call rlang::last_error() I get:

Backtrace:
  1. dplyr::select(., FGA, FTA, TOV, MP, TmFga, TmFta, TmTov, TmMin)
  9. tidyselect::vars_select(tbl_vars(.data), !!!enquos(...))
 10. tidyselect:::bad_calls(bad, "must evaluate to { singular(.vars) } positions or names, \\\n       not { first_type }")
 11. tidyselect:::glubort(fmt_calls(calls), ..., .envir = .envir)
 12. dplyr::select(., FGA, FTA, TOV, MP, TmFga, TmFta, TmTov, TmMin)

At this point, I am lost. What can I do to my code to work?

library(readxl)
Lakers_Overall_Stats <- read_excel("Desktop/Lakers Overall Stats.xlsx")
library(readxl)
Lakers_Record <- read_excel("Desktop/Lakers Record.xlsx")
require(dplyr)
require(ggplot2)

##WinPercentage of the Team after season
mydata <- Lakers_Record %>% select(Pts,Opp,W,L)%>%
  + mutate(wpct=Pts^13.91/(Pts^13.91+Opp^13.91),expwin=round(wpct*(W+L)),diff=W-expwin)
head(mydata)

##Specifiying 
Lakers_Overall_Stats[23,6] <- TmMin
Lakers_Overall_Stats[23,8] <- TmFga
Lakers_Overall_Stats[23,18] <- TmFta
Lakers_Overall_Stats[23,26] <- TmTov

rlang::last_error()

##Usage Percentage
Usgpct <- Lakers_Overall_Stats %>% select(FGA,FTA,TOV,MP,TmFga,TmFta,TmTov,TmMin)%>%
  + mutate(100*(Fga+0.44*Fta+Tov))*TmMin/(TmFga+0.44*TmFta+TmTov)*5(MP)
##head(Usgpct)
##filter(rank(desc(Usgpct))==1)

Also, am I filtering correctly? or should it be written as

Usgpct <- Lakers_Overall_Stats %>% select(FGA,FTA,TOV,MP,TmFga,TmFta,TmTov,TmMin)%>%
  filter(rank(desc(Usgpct))==1)%>%
  mutate(100*(Fga+0.44*Fta+Tov))*TmMin/(TmFga+0.44*TmFta+TmTov)*5(MP)
head(Usgpct)
Community
  • 1
  • 1
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 25 '19 at 16:57
  • **To make things easier** `df[23,6] <- A df[23,8] <- B df[23,18] <- C df[23,26] <- D` **##Usage Percentage** `Usgpct <- df %>% select(H,G,F,E,B,C,D,A)%>% filter(rank(desc(Usgpct))==1)%>% mutate(100*(H+0.44*G+F))*A/(B+0.44*C+D)*5(E) head(Usgpct)` – Jordan Reynolds Nov 25 '19 at 20:08
  • You seemed to have just changed variable names. I'm not sure that makes it any easier without any sample data. You should edit your question to make it reproducible rather than adding code or data as a comment. – MrFlick Nov 25 '19 at 20:16
  • Maybe, I'm not understanding however, even if I were to change the column numbers to df[1,2] <- A df[1,4] <- B df[1,9] <- C df[1,13] <- D and E, F, G, H were already listed how could I make this reproducible? – Jordan Reynolds Nov 26 '19 at 14:29
  • I understand that when calling mtcars[3,2] I will get the second column from the third row. However, that still does not answer why I am not able to save the second column in the third row as a specific variable so that I can use it for my equation. – Jordan Reynolds Nov 26 '19 at 14:30
  • Usgpct <- Lakers_Overall_Stats %>% select(FGA,FTA,TOV,MP,TmFga,TmFta,TmTov,TmMin)%>% filter(rank(desc(Usgpct))==1)%>% mutate(100*(Fga+0.44*Fta+Tov))*TmMin/(TmFga+0.44*TmFta+TmTov)*5(MP) #> Error in Lakers_Overall_Stats %>% select(FGA, FTA, TOV, MP, TmFga, TmFta, : could not find function "%>%" head(Usgpct) #> Error in head(Usgpct): object 'Usgpct' not found Created on 2019-11-26 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0) – Jordan Reynolds Nov 26 '19 at 19:13

1 Answers1

0

You have

Lakers_Overall_Stats[23,6] <- TmMin

This will modify the Lakers_Overall_Stats data frame by setting the element at 23,6 etc. to be TmMin. TmMin is an object outside of your data frame.

Maybe you want:

TmMin <- Lakers_Overall_Stats[23,6]

?

Also, you cannot select TmFga,TmFta,TmTov,TmMin since these variables are not part of your data frame. You can refer to those variables in your mutate equation, but because of the way you've set it up, they're stand-alone variables.

Arthur Yip
  • 5,810
  • 2
  • 31
  • 50
  • Thank you, Arthur! I am new to coding in R. How do I set this up so that they are not stand-alone variables? – Jordan Reynolds Nov 28 '19 at 02:47
  • What do you mean? Since they are single value variables, it doesn't seem that they belong in the table. What is "TmMin" etc.? Are they specific values per row? Or are they some summary statistic? – Arthur Yip Nov 28 '19 at 08:17