从数据框和 csv 输出中排除第 0 列(行名)列
Exclude column 0 (rownames) column from data frame and csv output
见鬼,
如何从数据框或 csv 输出中排除零列或行名列?
df <- df[,c(1,2,3,4,5]
drops <- c(0)
df <- df[,!(names(df) %in% drops)]
write.csv(df,'Forecast Values.csv')
我尝试了上面的方法,但行名列仍然会保留到 "df" 和 csv 文件中,我特别希望将它排除在 csv 中。同样值得注意的是,变量 'drops' 读取为零值,而不是列零。
您可以删除 dataframe
的 rownames
,只需将它们设置为空即可。它们默认为空(参见 ?data.frame
),但有时它们会被各种函数设置:
# 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
请注意,NULL
rownames
在打印时显示为增量计数,但不再存在于 a_data_frame
data frame
对象中,因此当由另一个函数处理时与 write.csv
一样,您看到的任何 rownames
都是由函数创建的,而不是从 data frame
对象本身获取的。
如前一位发帖人所述(参见?write.csv
),您可以在write.csv
命令中指定row.names=FALSE
。
您可以将行设置为 NULL,如下所示:
row.names(df) <- NULL
如果您愿意,也可以重命名行:
row.names(df) <- c("row_1","row_2", etc...)
见鬼,
如何从数据框或 csv 输出中排除零列或行名列?
df <- df[,c(1,2,3,4,5]
drops <- c(0)
df <- df[,!(names(df) %in% drops)]
write.csv(df,'Forecast Values.csv')
我尝试了上面的方法,但行名列仍然会保留到 "df" 和 csv 文件中,我特别希望将它排除在 csv 中。同样值得注意的是,变量 'drops' 读取为零值,而不是列零。
您可以删除 dataframe
的 rownames
,只需将它们设置为空即可。它们默认为空(参见 ?data.frame
),但有时它们会被各种函数设置:
# 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
请注意,NULL
rownames
在打印时显示为增量计数,但不再存在于 a_data_frame
data frame
对象中,因此当由另一个函数处理时与 write.csv
一样,您看到的任何 rownames
都是由函数创建的,而不是从 data frame
对象本身获取的。
如前一位发帖人所述(参见?write.csv
),您可以在write.csv
命令中指定row.names=FALSE
。
您可以将行设置为 NULL,如下所示:
row.names(df) <- NULL
如果您愿意,也可以重命名行:
row.names(df) <- c("row_1","row_2", etc...)