0

I am trying to study this dataset but whenever I use the str, head or summary functions it always comes back as NULL:

nhanes <- write.csv2("nhanes3.csv")

summary(nhanes)
str(nhanes)

 NULL
 NULL

Do you know anything that could help me get this fixed?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57

2 Answers2

3

write.csv2 will take a dataframe you have in R and save it as a csv. If you need to import a csv, you'll want to use read.csv or read.csv2 depending on the format of your file.

Lynn L
  • 523
  • 3
  • 7
  • I've tried `read.csv2()` and I get this `Error in read.table(file = file, header = header, sep = sep, quote = quote, : first five rows are empty: giving up` – diogofrosario May 01 '20 at 11:32
  • Maybe try `read.csv`? I assume it depends on how numbers are written where you are--whether . or , is used for decimals and what separator is used for the file. I've only ever used `read.csv` (. decimal and , separator) myself. `read.csv2` is for when your data uses ; as a separator and , for decimals, so if your data isn't in that format, it will likely return an error since it's probably not going to find the semi-colons its looking for. – Lynn L May 01 '20 at 11:38
  • Unfortunately got the same error back when I tried `read.csv()` – diogofrosario May 01 '20 at 11:42
  • I'm not sure what the error is, but I've been more successful reading poorly formatted `.csv` files using `fread()` from the `data.table` package. It may also be useful to crack open the file and verify the format (single versus double quotes for characters, comma or tab deliminator, stuff like that). – brknparticle May 01 '20 at 13:38
0

If the first five rows are empty, you need to skip those rows.

If your data has a header

df <- read.csv("path to your data", header=T, skip=5)

If your data doesn't have a header

df <- read.csv("path to your data", header=F, skip=5)
Andy
  • 413
  • 2
  • 15