Skip to content

Commit

Permalink
differences for PR #598
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 2, 2023
1 parent b123fcc commit 8b8eb90
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 104 deletions.
43 changes: 34 additions & 9 deletions 11-supp-read-write-csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,28 +362,46 @@ After altering our cars dataset by replacing 'Blue' with 'Green' in the `$Color`


```r
# Export the data. The write.csv() function requires a minimum of two
# arguments, the data to be saved and the name of the output file.
# Export the first rows of data. The write.csv() function requires a minimum of
# two arguments, the data to be saved and the name of the output file.

write.csv(carSpeeds, file = 'data/car-speeds-cleaned.csv')
write.csv(head(carSpeeds), file = 'data/car-speeds-cleaned.csv')
```

If you open the file, you'll see that it has header names, because the data had headers within R, but that there are numbers in the first column.

<img src="fig/01-supp-csv-with-row-nums.png" alt="csv written without row.names argument" />

```csv
"","Color","Speed","State"
"1","Blue",32,"NewMexico"
"2","Red",45,"Arizona"
"3","Blue",35,"Colorado"
"4","White",34,"Arizona"
"5","Red",25,"Arizona"
"6","Blue",41,"Arizona"
```

### The `row.names` Argument

This argument allows us to set the names of the rows in the output data file. R's default for this argument is `TRUE`, and since it does not know what else to name the rows for the cars data set, it resorts to using row numbers. To correct this, we can set `row.names` to `FALSE`:


```r
write.csv(carSpeeds, file = 'data/car-speeds-cleaned.csv', row.names = FALSE)
write.csv(head(carSpeeds), file = 'data/car-speeds-cleaned.csv', row.names = FALSE)
```

Now we see:

<img src="fig/01-supp-csv-without-row-nums.png" alt="csv written with row.names argument" />

```csv
"Color","Speed","State"
"Blue",32,"NewMexico"
"Red",45,"Arizona"
"Blue",35,"Colorado"
"White",34,"Arizona"
"Red",25,"Arizona"
"Blue",41,"Arizona"
```

::::::::::::::::::::::::::::::::::::::::: callout

Expand Down Expand Up @@ -428,17 +446,24 @@ Now we'll set `NA` to -9999 when we write the new .csv file:

```r
# Note - the na argument requires a string input
write.csv(carSpeeds,
write.csv(head(carSpeeds),
file = 'data/car-speeds-cleaned.csv',
row.names = FALSE,
na = '-9999')
```

And we see:

<img src="fig/01-supp-csv-with-special-NA.png" alt="csv written with -9999 as NA" />


```csv
"Color","Speed","State"
"Blue",32,"NewMexico"
"Red",45,"Arizona"
"Blue",-9999,"Colorado"
"White",34,"Arizona"
"Red",25,"Arizona"
"Blue",41,"Arizona"
```

:::::::::::::::::::::::::::::::::::::::: keypoints

Expand Down
94 changes: 0 additions & 94 deletions data/car-speeds-cleaned.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,3 @@
"White",34,"Arizona"
"Red",25,"Arizona"
"Blue",41,"Arizona"
"Blue",34,"NewMexico"
"Black",29,"Colorado"
"White",31,"Arizona"
"Red",26,"Colorado"
"Red",43,"Utah"
"White",26,"Utah"
"Blue",45,"Utah"
"Blue",49,"Utah"
"Black",33,"Utah"
"Red",53,"NewMexico"
"Blue",27,"Arizona"
"Blue",29,"NewMexico"
"White",27,"Arizona"
"Blue",42,"Utah"
"Blue",37,"Utah"
"Blue",42,"Utah"
"Red",48,"NewMexico"
"Blue",26,"Colorado"
"Red",40,"Colorado"
"Red",43,"NewMexico"
"Red",43,"Colorado"
"Red",25,"Utah"
"White",51,"Colorado"
"Blue",48,"Utah"
"Red",54,"NewMexico"
"White",36,"Colorado"
"Black",37,"Colorado"
"Red",38,"Utah"
"Black",33,"Colorado"
"Black",51,"Colorado"
"Blue",28,"NewMexico"
"Red",34,"Arizona"
"Black",54,"Utah"
"Red",47,"Colorado"
"Black",48,"Colorado"
"Black",53,"NewMexico"
"Red",32,"Arizona"
"Red",46,"Utah"
"White",34,"Arizona"
"Black",46,"Colorado"
"Blue",34,"NewMexico"
"Red",43,"NewMexico"
"Red",34,"NewMexico"
"Black",33,"Colorado"
"Black",43,"Arizona"
"Red",41,"Utah"
"White",42,"Utah"
"Red",45,"Arizona"
"Blue",52,"NewMexico"
"Blue",39,"Colorado"
"Black",29,"Utah"
"Blue",27,"Colorado"
"White",30,"Utah"
"Black",31,"Utah"
"Red",29,"Utah"
"Blue",27,"Colorado"
"Blue",50,"NewMexico"
"White",53,"Colorado"
"Black",50,"NewMexico"
"Red",46,"Colorado"
"Red",26,"NewMexico"
"Black",32,"Arizona"
"Blue",54,"NewMexico"
"White",36,"Utah"
"Blue",48,"Utah"
"Red",29,"Colorado"
"White",44,"Arizona"
"White",38,"Utah"
"Blue",49,"Colorado"
"Blue",52,"Colorado"
"Blue",36,"Colorado"
"Blue",45,"Arizona"
"Blue",34,"NewMexico"
"White",31,"Arizona"
"Black",44,"Utah"
"Blue",32,"Colorado"
"White",53,"Colorado"
"Black",34,"Arizona"
"Black",25,"Arizona"
"Red",41,"Arizona"
"Red",43,"Utah"
"White",41,"Arizona"
"White",40,"Utah"
"White",52,"Utah"
"White",44,"Utah"
"Red",34,"Arizona"
"Red",41,"Arizona"
"Red",31,"NewMexico"
"White",25,"Colorado"
"Black",46,"Colorado"
"White",34,"Utah"
"Black",25,"NewMexico"
"Black",32,"Arizona"
"White",42,"Utah"
2 changes: 1 addition & 1 deletion md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"episodes/08-making-packages-R.Rmd" "91c754a28acd4eae5d93ce5e3c45eb8c" "site/built/08-making-packages-R.md" "2023-11-21"
"episodes/09-supp-intro-rstudio.Rmd" "88e2bba9a474e81397ce83ba420702ba" "site/built/09-supp-intro-rstudio.md" "2023-11-21"
"episodes/10-supp-addressing-data.Rmd" "96b7a9778b2fee346bc98bc346faf51c" "site/built/10-supp-addressing-data.md" "2023-11-21"
"episodes/11-supp-read-write-csv.Rmd" "5435036cb83ce8f99519967d3d077e78" "site/built/11-supp-read-write-csv.md" "2023-11-21"
"episodes/11-supp-read-write-csv.Rmd" "9e1dd9128243bda65466b5f7787b119a" "site/built/11-supp-read-write-csv.md" "2023-12-02"
"episodes/12-supp-factors.Rmd" "721ea1b169ee105cef4702554714d800" "site/built/12-supp-factors.md" "2023-11-21"
"episodes/13-supp-data-structures.Rmd" "f0e68397ec97e4756af915c46c9f6439" "site/built/13-supp-data-structures.md" "2023-11-21"
"episodes/14-supp-call-stack.Rmd" "0949a3eaed6903c9aaf112d642e0d941" "site/built/14-supp-call-stack.md" "2023-11-21"
Expand Down

0 comments on commit 8b8eb90

Please sign in to comment.