从read_csv()中删除空列 [英] Remove empty columns from read_csv()

查看:664
本文介绍了从read_csv()中删除空列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读此处链接的csv文件使用readr包中的read_csv(),然后删除空列.

I am trying to read the csv file linked here using read_csv() from the readr package, and then remove empty columns.

如果我改用read.csv(),则可以很容易地使用以下内容删除空列8:12

If I use read.csv() instead, then the empty columns 8:12 can easily be removed using

library(dplyr)    
select(data, 1:7)

但是,当我使用read_csv()函数读取csv文件时,相同的代码给出了一个错误;

However, when I read the csv file using the read_csv() function, then the same code gives an error;

Error: found duplicated column name: NA, NA, NA, NA

如何删除这些空列?

正确命名空列似乎毫无意义,只是我可以将其删除.我宁愿使用read_csv()而不是read.csv(),因为这样以后在分析中会使生活变得更轻松.

It seems pointless to properly name empty columns just so I can remove them. I would prefer to use read_csv() rather than read.csv() as it makes life a bit easier later on in the analysis.

推荐答案

您可以这样做:

data <- data[,apply(data, 2, function(x) { sum(!is.na(x)) > 0 })]

这将仅保留不完全为NA的列.

This will keep only columns which are not entirely NA.

或者,如果您安装了dplyr 0.5,则可以使用新的select_if函数来达到相同的效果:

Or, if you have dplyr 0.5 installed, you can use the new select_if function to achieve the same effect:

has_data <- function(x) { sum(!is.na(x)) > 0 }
data <- data %>% select_if(has_data)

这篇关于从read_csv()中删除空列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆