이해하기 어려운 R Errors message

Published by onesixx on

https://www.r-bloggers.com/translating-weird-r-errors/
dd <- data.frame(cbind(c(1,2,3),c(5,4,3)))
names(dd) <- c("a","b")

replacement has 0 rows, data has 3

컬럼a 를 c로 착각해서 사용했을때, 나오는 Error 메세지인데 참 해석하기 힘들다.
그냥 c라는 컬럼이 없다라고 나오면 될텐데...

dd$d <- as.numeric(dd$c==2)

Error in `$<-.data.frame`(`*tmp*`, d, value = numeric(0)) : 
  replacement has 0 rows, data has 3

다른예

dd <- data.table()
dd$a <- seq.Date(as.Date("2020-01-01"), as.Date("2020-01-10"), "days")
dd$b <- as.vector(seq(1:10))

d1 <- data.table()
d1[ ,a:= seq.Date(as.Date("2020-01-01"), as.Date("2020-01-10"), "days")]
d1[ ,b:= as.vector(seq(1:10))]

d2 <- data.table(a=seq.Date(as.Date("2020-01-01"), as.Date("2020-01-10"), "days"),
                 b=as.vector(seq(1:10)))

> identical(dd,d1)
[1] TRUE
> identical(dd,d2)
[1] FALSE
             a  b
 1: 2020-01-01  1
 2: 2020-01-02  2
 3: 2020-01-03  3
 4: 2020-01-04  4
 5: 2020-01-05  5
 6: 2020-01-06  6
 7: 2020-01-07  7
 8: 2020-01-08  8
 9: 2020-01-09  9
10: 2020-01-10 10
> rowSums(dd$b)
Error in rowSums(dd$b) : 
  'x' must be an array of at least two dimensions
> dd %>% ggplot(aes(x=a, y=b)) + geom_line()
Error in `$<-.data.frame`(x, name, value) : 
  replacement has 1 row, data has 0
> attributes(dd)
$class
[1] "data.table" "data.frame"
$row.names
integer(0)
$names
[1] "a" "b"
$.internal.selfref


> attributes(d2)
$names
[1] "a" "b"
$row.names
 [1]  1  2  3  4  5  6  7  8  9 10
$class
[1] "data.table" "data.frame"
$.internal.selfref
> row.names(d1)
character(0)
> row.names(d2)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

> attr(dd, "row.names")
integer(0)
> attr(d2, "row.names")
 [1]  1  2  3  4  5  6  7  8  9 10
> attr(dd, "row.names") <- attr(d2, "row.names")
==> NO ERROR 

all arguments must have the same length

misspell 인데, 차라리 aa라는 컬럼이 없다고 나온면 에러를 찾기 쉬울텐데...

> dd
  a 
1 1 5
2 2 4
3 3 3
# misspell a to aa
table(dd$aa, dd$b)

Error in table(dd$aa, dd$b) : 
  all arguments must have the same length
Categories: R Reshaping

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x