Hell all,
How do I go about excluding the zero column, or rownames column from a data frame or csv output?
df <- df[,c(1,2,3,4,5]
drops <- c(0)
df <- df[,!(names(df) %in% drops)]
write.csv(df,'Forecast Values.csv')
I tried the above but the rownames column still carries over into both "df" and the csv file, and I particularly want it excluded in the csv. Also of note, variable 'drops' reads as a value of zero, as opposed to the column zero.
You can drop the rownames
of a dataframe
by simply setting them to null. They're null by default (see ?data.frame
), but sometimes they get set by various functions:
# Create a sample dataframe with row.names
a_data_frame <- data.frame(letters = c("a", "b", "c"),
numbers = c(1, 2, 3),
row.names = c("Row1", "Row2", "Row3"));
# View it
a_data_frame;
> letters numbers
>Row1 a 1
>Row2 b 2
>Row3 c 3
# Use rownames() to set them to new value, which is null in this case
rownames(a_data_frame) <- NULL;
# View it again
a_data_frame;
> letters numbers
>1 a 1
>2 b 2
>3 c 3
Note that NULL
rownames
show up as an incremental count when printed, but no longer exist in the a_data_frame
data frame
object, so when handled by another function like write.csv
, any rownames
that you see are created by the function, not taken from the data frame
object itself.
As mentioned by the previous poster (see ?write.csv
), you can specify row.names=FALSE
in your write.csv
command.