0

I'm trying to use a code chunk but I keep getting this error: Error: attempt to use zero-length variable name

enter image description here

#install.packages("tidyverse")
library(ggplot2)
#install.packages("rmarkdown")
library(rmarkdown)

```{r, echo=FALSE}
ggplot(Men_DF, mapping=aes(x=height,y=emails))+geom_point(size=1)
``` 

This is what the data set looks likeenter image description here

Does anyone know why I'm getting this error?

dput(head(Men_DF, 10))
structure(list(emails = c(1L, 3L, 9L, 5L, 19L, 0L, 2L, 3L, 1L, 
4L), rating = c(0.6125247, -0.100942, 0.2975212, -0.0284386, 
0.4072408, -0.3510217, -0.0468019, -0.7681106, -0.4641442, 0.2387305
), height = c(71.5, 67.5, 69.5, 69.5, 69.5, 75.5, 71.5, 65.5, 
69.5, 69.5), bmi = c(29.56526, 27.00137, 25.4697, 26.92511, 24.01428, 
25.28222, 25.43987, 31.95268, 22.55887, 26.92511), yrs_education = c(14, 
18, 16, 21, 21, 14, 16, 17.5, 17.5, 12.5), age = c(3L, 1L, 3L, 
1L, 3L, 2L, 2L, 2L, 1L, 2L), days_active = c(8L, 74L, 30L, 67L, 
61L, 70L, 20L, 108L, 108L, 43L)), row.names = c(NA, 10L), class = "data.frame")


> str(Men_DF)
'data.frame':   1507 obs. of  7 variables:
 $ emails       : int  1 3 9 5 19 0 2 3 1 4 ...
 $ rating       : num  0.6125 -0.1009 0.2975 -0.0284 0.4072 ...
 $ height       : num  71.5 67.5 69.5 69.5 69.5 75.5 71.5 65.5 69.5 69.5 ...
 $ bmi          : num  29.6 27 25.5 26.9 24 ...
 $ yrs_education: num  14 18 16 21 21 14 16 17.5 17.5 12.5 ...
 $ age          : int  3 1 3 1 3 2 2 2 1 2 ...
 $ days_active  : int  8 74 30 67 61 70 20 108 108 43 ...
camille
  • 16,432
  • 18
  • 38
  • 60
  • did you try these solutions? https://stackoverflow.com/questions/46171362/rmarkdown-error-attempt-to-use-zero-length-variable-name Without a reproducible example it's hard to say – Jonni Jan 07 '22 at 03:42
  • I have, but these didn't work. I added a picture of what the data set looks like, if that helps! – helpneededthankyou Jan 07 '22 at 04:14
  • maybe something to do with the existing Men_DF in your environment? if csv import this could cause issue https://stackoverflow.com/questions/31385976/error-attempt-to-use-zero-length-variable-name – Jonni Jan 07 '22 at 07:17
  • hmm doesn't seem the be the issue, thanks though! – helpneededthankyou Jan 07 '22 at 17:15
  • If that's the entirety of your Rmarkdown, you're probably having a scoping issue. Rmarkdown generally knits in a fresh environment in order to make your documents fully reproducible. So your data needs to be defined within the scope of the document. I'm not getting any issue from your example. You also seem to be trying to load libraries outside of any R chunk, in which case it's just text – camille Jan 07 '22 at 23:48

1 Answers1

1

It could be a problem of the environment. I have just reloaded everything as you edited in the post and I don't have problems..

---
title: "Untitled"
author: "Author"
date: "8/1/2022"
output: html_document
---
```{r}
Men_DF <- structure(list(emails = c(1L, 3L, 9L, 5L, 19L, 0L, 2L, 3L, 1L, 
4L), rating = c(0.6125247, -0.100942, 0.2975212, -0.0284386, 
0.4072408, -0.3510217, -0.0468019, -0.7681106, -0.4641442, 0.2387305
), height = c(71.5, 67.5, 69.5, 69.5, 69.5, 75.5, 71.5, 65.5, 
69.5, 69.5), bmi = c(29.56526, 27.00137, 25.4697, 26.92511, 24.01428, 
25.28222, 25.43987, 31.95268, 22.55887, 26.92511), yrs_education = c(14, 
18, 16, 21, 21, 14, 16, 17.5, 17.5, 12.5), age = c(3L, 1L, 3L, 
1L, 3L, 2L, 2L, 2L, 1L, 2L), days_active = c(8L, 74L, 30L, 67L, 
61L, 70L, 20L, 108L, 108L, 43L)), row.names = c(NA, 10L), class = "data.frame")

```


```{r}
#install.packages("tidyverse")
library(ggplot2)
#install.packages("rmarkdown")
library(rmarkdown)
```

```{r, echo=FALSE}
ggplot(Men_DF, mapping=aes(x=height,y=emails))+geom_point(size=1)
``` 

image

emr2
  • 1,436
  • 7
  • 23
  • I'm a complete novice here, so just to confirm, when you say format, are you asking if I'm working in rmarkdown format? If so, I am! The format is saved as .rmd – helpneededthankyou Jan 07 '22 at 17:09
  • @helpneededthankyou maybe I explained it in the wrong way, sorry. But generally I was asking you if you checked how the columns were. If you are working with the columns `height` and `emails`, you need to be sure that they are in the same format. That is if both are numerical and not factors, for example. To check the format of the dataframe you do `str(dataframe)` as you can see in the solution that I posted. – emr2 Jan 07 '22 at 18:30
  • Oh, got it! So running the str(dataframe) command you showed, I'm seeing that emails is an integer and bmi is a number. Is that a problem? – helpneededthankyou Jan 07 '22 at 18:41
  • @helpneededthankyou But.. you are not using bmi to run the plot... So it doesn't mind if is numerical. I have transformed email into integer but I still get the plot. Maybe you have tried it, but... have you restarted the session and try it again? Some errors that I get from R are solved because of this... On the other hand, how big is the file? could you type `dput(dataframe)` and upload into the post to have the exact dataframe, just in case I copied something wrong? – emr2 Jan 07 '22 at 22:47
  • @helpneededthankyou If you don't want to post the entire dataframe, you can do `dput(head(dataframe, 10))` for example. – emr2 Jan 07 '22 at 22:48
  • @helpneededthankyou could you add the info that you get from `str(dataframe)`, please? just in case I can see something that we don't see. – emr2 Jan 07 '22 at 22:53
  • I added it to the initial post, thanks so much for your help! – helpneededthankyou Jan 07 '22 at 23:39
  • I'm running into this issue anytime I have code chunks like ```{r}``` not sure if that information helps at all – helpneededthankyou Jan 07 '22 at 23:45
  • @helpneededthankyou I don't really know what it is going on, to be honest. I tried to load everything again and I don't have any problems to get the plot. Have you tried to clean the complete environment, restart R.... everything? Sorry for not being able to help you more. – emr2 Jan 07 '22 at 23:53
  • @helpneededthankyou I edited the answer, but it is not something new from your post. I have created a R markdown file and it works perfectly... I have this R version (4.0.3 (2020-10-10)), but I don't think it can be the problem because I tried with a newer one (4.1) and it worked... Sorry – emr2 Jan 08 '22 at 00:00
  • It's okay, thanks so much for your help! I'll try to restart again and see! – helpneededthankyou Jan 08 '22 at 00:06