data frame entries display only <dbl [1]> instead of values

In the following Rmarkdown chunk in my copy of RStudio, the inline chunk output displays entries that look like <dbl [1]> instead of values. When I knit the chunk to pdf, the correct values are displayed. Can I get the inline chunk output to display that way?

library(ISLR)
data(Auto)
as.data.frame(t(sapply(Auto[,c(1,3:7)],function(bla)
    list(means=mean(bla),sds=sd(bla),ranges=range(bla)))))

The inline output looks like this

                                  means               sds               ranges
                                   <list>                 <list>             <list>
mpg                         <dbl [1]>          <dbl [1]>     <dbl [2]>
displacement      <dbl [1]>          <dbl [1]>     <dbl [2]>
horsepower          <dbl [1]>          <dbl [1]>     <dbl [2]>
weight                     <dbl [1]>          <dbl [1]>     <dbl [2]>

except that it is lined up better.

Can you share the Rmd content in which you are using this ?

By inline chunk output you mean using

inline content in like in `r fun()`

I prefer to confirm with you because inline chunks are expecting character vector or object that can be converted to character vector. And data.frame is not really in this category.

So can you share a full Rmd example so that I can look into this ?

Thanks

Here is the entire file. I don't think I have the right to add attachments, so I'm adding it inline.


title: 'Exercise XXX'
author: 'me'
date: '2020-05-29'
output:
pdf_document:
latex_engine: xelatex

instructions redacted

library(ISLR)
data(Auto)
attach(Auto)
str(Auto)
nrow(Auto)
ncol(Auto)
Auto <- na.omit(Auto)
nrow(Auto)
ncol(Auto)
colnames(Auto)
with(Auto,cylinders <- as.factor(cylinders))
with(Auto,origin <- as.factor(origin))

instructions redacted

summary(Auto)
table(Auto$mpg)

Quantitative: mpg, displacement, horsepower, weight, acceleration, year

Qualitative: cylinders, origin, name

instructions redacted

range(Auto$mpg)
range(Auto$displacement)
sapply(Auto[,c(1,3:7)],range)

instructions redacted

options(width=40)
options(digits=3)
sd(Auto$mpg)
sapply(Auto[,c(1,3:7)],mean)
sapply(Auto[,c(1,3:7)],sd)
as.data.frame(t(sapply(Auto[,c(1,3:7)],function(bla)
											 list(means=mean(bla),sds=sd(bla),ranges=range(bla)))))

instructions redacted

as.data.frame(t(sapply(Auto[-10:-85,c(1,3:7)],function(bla)
											 list(means=mean(bla),sds=sd(bla),ranges=range(bla)))))

instructions redacted

pairs(Auto[,c(1,3:7)])
library(GGally)
#install.packages("ggplot2")
#install.packages("fansi")
library(ggplot2)
ggpairs(Auto[,c(1,3:7)],aes(alpha=0.4))
plot(Auto$displacement,Auto$horsepower)
plot(Auto$weight,Auto$mpg)
plot(Auto$year,Auto$mpg)

The strongest linear relationship is between displacement and horsepower. The next strongest linear relationships seem to be between displacement and weight and between horsepower and weight. There is a strong categorical relationship between displacement and cylinders. There is a somewhat strong categorical relationship between displacement and origin. There are strong curvilinear relationships between mpg and displacement, mpg and horsepower, and mpg and weight. The other relationships are less clear.

instructions redacted

with(Auto,plot(weight,mpg))
with(Auto,plot(displacement,mpg))
with(Auto,plot(horsepower,mpg))
library(graphics)
with(Auto,scatter.smooth(weight,mpg))
p <- ggplot(Auto,aes(weight,mpg))+ geom_point()
p
p+geom_smooth(method=lm)
p+geom_smooth(method=loess)

Weight appears to be the best predictor of mpg. A scatterplot shows that only two 3500 lb+ cars get better than 20 mpg, while no 40 mpg+ car weighs more than 2500 lbs. Most cars show a relationship between mpg and weight on a smooth curve. Displacement and horsepower also appear to be good predictors, especially for larger values of displacement and horsepower.

I meant to add that I was probably using the term "inline" incorrectly. I meant the way the output of code appears in the RStudio window right after the code chunk when I run the chunk.

Hi, welcome!

Please have a look to our homework policy, homework inspired questions are welcome but they should not include verbatim instructions from your course.

1 Like

Sorry. I did not think about this issue. I have redacted all the instructions from the post. I only posted the whole thing because someone asked for it. I don't think the instructions were relevant to my display issue anyway.

I think you want to replace this sort of pattern:

# not so great
as.data.frame(t(sapply(iris[, c(1, 3:4)], function(bla) {
  list(means = mean(bla), sds = sd(bla), ranges = range(bla))
}))) %>% tibble()

with this sort of one :

summarise(iris, across(
  .cols = c(1, 3:4),
  .fns = list(
    mean = mean, 
    sds = sd, 
    range_lo = ~ range(.)[[1]],
    range_hi = ~ range(.)[[2]]
  ),
  .names = "{.col}@{.fn}"
))%>% pivot_longer(cols=everything(),
                   names_pattern = "(.*)@(.*)",
                   names_to=c("col","fn")) %>%
  pivot_wider( names_from="fn" )