1

I am a beginner in R trying to analyze NBA draft data. I am trying to reassign the value of how many people attended the college(freq) to the name of the college(y2015$college_name) in the data set.

Here is some example code:

urlfile <- "https://raw.githubusercontent.com/kshvmdn/nbadrafts/master/datasets/2015_nbadraft.csv"


y2015 <- read.csv(url(urlfile))
freq <- count(y2015$college_name)

if(draft$college_name[i] == freq[i, 1]){
draft$college_name[i] <- freq[i, 2]
} else {
draft$college_name[i] <- 0
}

I just picked i to be the variable for no reason and I keep getting the error: object 'i' not found

What do I do?

DJJ
  • 2,481
  • 2
  • 28
  • 53
richard
  • 11
  • 3
  • 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. Sounds like one of these variables are not what they seem to be. Did you actually define `n`? What do you mean by "random variable" here? Sounds like you probably have `dplyr` loaded and it's finding the `n()` function. – MrFlick Jul 02 '20 at 20:37
  • @MrFlick Is this better? – richard Jul 02 '20 at 20:54
  • You can't just use a variable `i` if you didn't define it. What are you really trying to do here? What is the desired output? Do you have any non-base R packages loaded? – MrFlick Jul 02 '20 at 20:56
  • @Mr.Flick I am trying to change the names in the "college_name" column to the number of players in the draft from that college. what should I define I as? – richard Jul 02 '20 at 20:58
  • Since you seem to have `dplyr` loaded, you can create a new column with `y2015 %>% group_by(college_name) %>% mutate(number_in_draft=n())`. – MrFlick Jul 02 '20 at 21:00
  • I also would suggest this [doc](https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf). It might save you a lot of frustration in the future. – DJJ Jul 02 '20 at 21:05
  • @Mr.Flick, I get Error: `n()` must only be used inside dplyr verbs. When I run the code – richard Jul 02 '20 at 21:07
  • I don't understand. `mutate()` is a dplyr verb. Do you have another package loaded that also defines `mutate()`? I can't reproduce with the info given. – MrFlick Jul 02 '20 at 21:12
  • I have plyr loaded – richard Jul 02 '20 at 21:18
  • Is there a reason you are still using `plyr`? That's been mostly superseded by `dplyr`. If you want to use both, just make sure to load `plyr` before `dplyr`. They both define `mutate()` – MrFlick Jul 02 '20 at 21:53

0 Answers0