I would like to delete a row from a data frame and sum the resulting columns. I know the row I want to delete based on its contents, but not its row number. Below I present three examples, two of which work. Using -
to delete the row only works if the first row is to be deleted. Why is that?
My question is similar to this one: How to delete the first row of a dataframe in R? However, there the row is deleted based on its row number.
# This works.
state = 'OH'
my.data = read.table(text = "
county y1990 y2000
cc NA 2
OH NA 10
bb NA 1
", sep = "", header = TRUE, na.strings = "NA", stringsAsFactors = FALSE)
my.colsums2 <- colSums(my.data[!(my.data$county == state), 2:ncol(my.data)], na.rm=TRUE)
my.colsums2
# y1990 y2000
# 0 3
# This works.
my.data = read.table(text = "
county y1990 y2000
OH NA 10
cc NA 2
bb NA 1
", sep = "", header = TRUE, na.strings = "NA", stringsAsFactors = FALSE)
my.colsums2 <- colSums(my.data[-(my.data$county == state), 2:ncol(my.data)], na.rm=TRUE)
my.colsums2
# y1990 y2000
# 0 3
# This does not work.
my.data = read.table(text = "
county y1990 y2000
cc NA 2
OH NA 10
bb NA 1
", sep = "", header = TRUE, na.strings = "NA", stringsAsFactors = FALSE)
my.colsums2 <- colSums(my.data[-(my.data$county == state), 2:ncol(my.data)], na.rm=TRUE)
my.colsums2
# y1990 y2000
# 0 11
I guess I am still confused over the difference between !
and -
. Thank you for any advice.