Lukar Huang
Lukar Huang

Reputation: 107

longer object length is not a multiple of shorter object length [error]

So I am currently getting an error

Error in data.frame(PTS, P3M, REB = OREB + DREB, AST, TOV, STL, BLK, PF) : 
arguments imply differing number of rows: 605, 30
In addition: Warning message:
In OREB + DREB :
longer object length is not a multiple of shorter object length

My code is as follows in R, version 3.5.3

library('BasketballAnalyzeR')
RNGkind(sample.kind = "Rounding")
attach(Pbox)
View(Pbox)
data <- data.frame(PTS, P3M, REB=OREB+DREB, AST, TOV, STL, BLK, PF)
NROW(OREB)
detach(Pbox)

I'm confused how NROW(OREB) gives me a row count of 361 and NROW(DREB) gives me a row count of 605. I saved the data as a .csv file and OREB and DREB columns perfectly have 605 values in their respective columns. I've seen similar questions regarding the same error but didn't find a solution. I've tried reinstalling the packet, restart my computer (lol), restart R, since it logically does not make sense to me

Upvotes: 0

Views: 203

Answers (1)

user2554330
user2554330

Reputation: 44788

You should almost never use attach(), because it leads to confusion like this. What attach(Pbox) does is to put Pbox second on the search list, after the global environment. You can see the variables in the global environment by running

ls()

I'd guess one of the Pbox column names is also a variable in the global environment, and is being found first. You probably ignored a warning like The following object is masked _by_ .GlobalEnv: OREB.

A better approach if you want to save typing is

data <- with(Pbox, data.frame(PTS, P3M, REB=OREB+DREB, AST, TOV, STL, BLK, PF))

This puts Pbox ahead of the global environment for the duration of that line, so you'll find the variables you expect to be finding.

Upvotes: 1

Related Questions