我在那个帖子上 read.csv and skip last column in R但没有找到我的答案,并尝试直接在 Answer 中检查......但这不是正确的方法(感谢 mjuarez 花时间让我回到正轨。
原来的问题是:
I have read several other posts about how to import csv files with read.csv but skipping specific columns. However, all the examples I have found had very few columns, and so it was easy to do something like:
columnHeaders <- c("column1", "column2", "column_to_skip") columnClasses <- c("numeric", "numeric", "NULL") data <- read.csv(fileCSV, header = FALSE, sep = ",", col.names = columnHeaders, colClasses = columnClasses)
所有答案都很好,但不适用于我打算做的事情。所以我问我自己和其他人:
And in one function, does
data <- read_csv(fileCSV)[,(ncol(data)-1)]
could work?
我试过一行
R
上车 data
,前 6 列中的所有 5 列,所以不是最后一列。为此,我想在列数中使用“-”,您认为可能吗?我怎样才能做到这一点?谢谢!
最佳答案
在基地 r
它必须是2步操作。例子:
> data <- read.csv("test12.csv")
> data
# 3 columns are returned
a b c
1 1/02/2015 1 3
2 2/03/2015 2 4
# last column is excluded
> data[,-ncol(data)]
a b
1 1/02/2015 1
2 2/03/2015 2
一个不会写
data <- read.csv("test12.csv")[,-ncol(data)]
在基地 r
.但是,如果您知道
csv
中的最大列数(在我的情况下说 3)然后可以写:df <- read.csv("test12.csv")[,-3]
df
a b
1 1/02/2015 1
2 2/03/2015 2
关于r - 使用 read.csv 跳过 r 中的最后一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48597844/