[R] Bug in print for data frames?

Christian Asseburg rhe|p @end|ng |rom mo|n@||
Wed Oct 25 08:18:26 CEST 2023


Hi! I came across this unexpected behaviour in R. First I thought it was a bug in the assignment operator <- but now I think it's maybe a bug in the way data frames are being printed. What do you think?

Using R 4.3.1:

> x <- data.frame(A = 1, B = 2, C = 3)
> y <- data.frame(A = 1)
> x
  A B C
1 1 2 3
> x$B <- y$A # works as expected
> x
  A B C
1 1 1 3
> x$C <- y[1] # makes C disappear
> x
  A B A
1 1 1 1
> str(x)
'data.frame':   1 obs. of  3 variables:
 $ A: num 1
 $ B: num 1
 $ C:'data.frame':      1 obs. of  1 variable:
  ..$ A: num 1

Why does the print(x) not show "C" as the name of the third element? I did mess up the data frame (and this was a mistake on my part), but finding the bug was harder because print(x) didn't show the C any longer.

Thanks. With best wishes -

. . . Christian



More information about the R-help mailing list