Skip to content
Prev 395351 / 398502 Next

Bug in print for data frames?

The "problem" goes away if you use

x$C <- y[1,]

If you have another row in your x, say:
x <- data.frame(A=c(1,4), B=c(2,5), C=c(3,6))

then your code
x$C <- y[1]
returns an error.

If y has the same number of rows as x$C then R has the same outcome as in your example.

It looks like your code tells R to replace all of column C (including the name) with all of vector y.

Maybe unexpected, but not a bug. It is consistent.


-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Rui Barradas
Sent: Thursday, October 26, 2023 6:43 AM
To: Christian Asseburg <rhelp at moin.fi>; r-help at r-project.org
Subject: Re: [R] Bug in print for data frames?

[External Email]

?s 07:18 de 25/10/2023, Christian Asseburg escreveu:
Hello,

To expand on the good answers already given, I will present two other example data sets.

Example 1. Imagine that instead of assigning just one column from y to x$C you assign two columns. The result is a data.frame column. See what is displayed as the columns names.
And unlike what happens with `[`, when asssigning columns 1:2, the operator `[[` doesn't work. You will have to extract the columns y$A and y$B one by one.



x <- data.frame(A = 1, B = 2, C = 3)
y <- data.frame(A = 1, B = 4)
str(y)
#> 'data.frame':    1 obs. of  2 variables:
#>  $ A: num 1
#>  $ B: num 4

x$C <- y[1:2]
x
#>   A B C.A C.B
#> 1 1 2   1   4

str(x)
#> 'data.frame':    1 obs. of  3 variables:
#>  $ A: num 1
#>  $ B: num 2
#>  $ C:'data.frame':   1 obs. of  2 variables:
#>   ..$ A: num 1
#>   ..$ B: num 4

x[[1:2]]  # doesn't work
#> Error in .subset2(x, i, exact = exact): subscript out of bounds



Example 2. Sometimes it is usefull to get a result like this first and then correct the resulting df. For instance, when computing more than one summary statistics.

str(agg)  below shows that the result summary stats is a matrix, so you have a column-matrix. And once again the displayed names reflect that.

The trick to make the result a df is to extract all but the last column as a sub-df, extract the last column's values as a matrix (which it is) and then cbind the two together.

cbind is a generic function. Since the first argument to cbind is a sub-df, the method called is cbind.data.frame and the result is a df.



df1 <- data.frame(A = rep(c("a", "b", "c"), 5L), X = 1:30)

# the anonymous function computes more than one summary statistics # note that it returns a named vector agg <- aggregate(X ~ A, df1, \(x) c(Mean = mean(x), S = sd(x))) agg
#>   A    X.Mean       X.S
#> 1 a 14.500000  9.082951
#> 2 b 15.500000  9.082951
#> 3 c 16.500000  9.082951

# similar effect as in the OP, The difference is that the last # column is a matrix, not a data.frame
str(agg)
#> 'data.frame':    3 obs. of  2 variables:
#>  $ A: chr  "a" "b" "c"
#>  $ X: num [1:3, 1:2] 14.5 15.5 16.5 9.08 9.08 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:2] "Mean" "S"

# nc is just a convenience, avoids repeated calls to ncol nc <- ncol(agg) cbind(agg[-nc], agg[[nc]])
#>   A Mean        S
#> 1 a 14.5 9.082951
#> 2 b 15.5 9.082951
#> 3 c 16.5 9.082951

# all is well
cbind(agg[-nc], agg[[nc]]) |> str()
#> 'data.frame':    3 obs. of  3 variables:
#>  $ A   : chr  "a" "b" "c"
#>  $ Mean: num  14.5 15.5 16.5
#>  $ S   : num  9.08 9.08 9.08



If the anonymous function hadn't returned a named vetor, the new column names would have been "1". "2", try it.


Hope this helps,

Rui Barradas



--
Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus.
http://www.avg.com/

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.r-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.