cultivating & crashing

an organic collection of notes, observations, and thoughts

Category: code

do not attach()

Note to self: DO NOT USE attach(). Do whatever it takes to avoid it.

I would up making a mess of my variables, to the point were the variables of the dataframe I was using were masking themselves four times over.

In case this happens in the future, to get out of the mess:

  1. locate the code you used to create dataframe
  2. figure out how you’ve manipulated the variables since you attached the dataframe they came from so you can run these transformations again later, so you can keep doing what you were supposed to be doing in the first place instead of messing up your Global Environment
  3. search() to look at what you currently have sitting in your path. you will see copies of the attached dataframe. the more copies, the more silly you were
  4. rm() variables that should be IN the dataframe that you cavalierly created outside of it, like a numbnuts
  5. detach(dataframe) so package it back up
  6. rm(dataframe) to delete the whole goddamn thing (one iteration of it, that is)
  7. repeat steps 2-4 until you see no more iterations of the dataframe in your path
  8. re-create dataframe using code from step 1
  9. correct all code to include dataframe$ before variable names
  10. repeat all of your manipulations from step 2
  11. resist the temptation to attach() dataframes ever again

Notes

# rename columns
> View(ghq_elsa)
> ghq_elsa <- setNames(ghq_elsa, c("ghqconc", "ghqsleep", "ghquse", "ghqdecis", "ghqstrai", "ghqover", "ghqenjoy", "ghqface", "ghqunhap", "ghqconfi", "ghqworth", "ghqha"))

# export to .txt file
> write.table(ghq_elsa, "c:/[path]/data.txt", sep="\t")

Mass shootings dataset

First small barplot on the mass shootings dataset. Frustrated that I don’t know why the Saturday label won’t appear.

barplot1

# obtaining counts for shootings per day
cur15$day Friday Monday Saturday Sunday Thursday Tuesday Wednesday
35 31 72 68 28 31 31

# creating dataframe
weekday = c(“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”)
count = c(68, 31, 31, 31, 28, 35, 72)
wkday.count = data.frame(weekday, count)

# barplot, days not in alphabetical order
library(ggplot2)
wkday.count$weekday ggplot(wkday.count, aes(wkday.count$weekday, y= wkday.count$count)) +
geom_bar(stat=”identity”, fill = “dark grey”, colour = “black”, alpha = 1/3) +
ggtitle(“Mass shootings in 2015 (as of October 7)”) + xlab(“Day of the week”) +
ylab(“Number of mass shootings”)

Note to self

# count of non-NAs in dataframe “cog”
colSums(!is.na(cog))

# write first 50 rows of dataset to csv file
w2ktn <- w2[c(1:50),]
View(w2ktn)
write.csv(w2ktn, “w2ktn.csv”, row.names=TRUE)

# get column index from dataframe
which(colnames(w2ktn)==”TDIABET”)

# drop column from dataframe
w2ktn$diab <- NULL