如何检查数据框中的所有行是否为空或具有NaN值?
我的测试数据:
structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "HK6", class = "factor"),
code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "HK6", class = "factor"),
date = structure(c(1492905600, 1492909200, 1492912800, 1492916400,
1492920000, 1492923600, 1492927200, 1492930800, 1492934400,
1492938000, 1492941600, 1492945200, 1492948800, 1492952400,
1492956000, 1492959600, 1492963200, 1492966800, 1492970400,
1492974000, 1492977600, 1492981200, 1492984800, 1492988400,
1492992000, 1492995600, 1492999200, 1493002800, 1493006400,
1493010000, 1493013600, 1493017200, 1493020800, 1493024400,
1493028000, 1493031600, 1493035200, 1493038800, 1493042400,
1493046000, 1493049600, 1493053200, 1493056800, 1493060400,
1493064000, 1493067600, 1493071200, 1493074800, 1493078400,
1493082000, 1493085600, 1493089200, 1493092800, 1493096400,
1493100000, 1493103600, 1493107200, 1493110800, 1493114400,
1493118000, 1493121600, 1493125200, 1493128800, 1493132400,
1493136000, 1493139600, 1493143200, 1493146800, 1493150400,
1493154000, 1493157600, 1493161200, 1493164800, 1493168400,
1493172000, 1493175600, 1493179200, 1493182800, 1493186400,
1493190000, 1493193600, 1493197200, 1493200800, 1493204400,
1493208000, 1493211600, 1493215200, 1493218800, 1493222400,
1493226000, 1493229600, 1493233200, 1493236800, 1493240400,
1493244000, 1493247600, 1493251200, 1493254800, 1493258400,
1493262000, 1493265600, 1493269200, 1493272800, 1493276400,
1493280000, 1493283600, 1493287200, 1493290800, 1493294400,
1493298000, 1493301600, 1493305200, 1493308800, 1493312400,
1493316000, 1493319600, 1493323200, 1493326800, 1493330400,
1493334000, 1493337600, 1493341200, 1493344800, 1493348400,
1493352000, 1493355600, 1493359200, 1493362800, 1493366400,
1493370000, 1493373600, 1493377200, 1493380800, 1493384400,
1493388000, 1493391600, 1493395200, 1493398800, 1493402400,
1493406000, 1493409600, 1493413200, 1493416800, 1493420400
), class = c("POSIXct", "POSIXt"), tzone = "GMT"), PM25 = c(NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN)), row.names = c(NA,
-144L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), .Names = c("site", "code", "date", "PM25"), vars = list(site,
code), drop = TRUE, indices = list(0:143), group_sizes = 144L, biggest_group_size = 144L, labels = structure(list(
site = structure(1L, .Label = "HK6", class = "factor"), code = structure(1L, .Label = "HK6", class = "factor")), row.names = c(NA,
-1L), class = "data.frame", vars = list(site, code), drop = TRUE, .Names = c("site",
"code")))
列PM25
的所有行都是NaN,如何检查它们是否都是NaN,然后执行某些操作?
PM25
的列是动态的 - 有时是PM10
或其他任何内容。
有什么想法吗?
答案 0 :(得分:3)
我无法加载您的数据,我收到错误:
object 'site' not found
但是,您可以找到所有完全NA的列:
columns = which(colSums(is.na(df)) == nrow(df))
返回完全NA的列的索引。然后,您可以使用以下内容删除这些列:
df = df[,-columns]
希望这有帮助!
编辑:根据Wen和lmo的建议改变了专栏。
答案 1 :(得分:3)
示例数据
my.df <- data.frame(a=c(1, NA, 3), b=c(5, NA, NaN))
my.df
# a b
# 1 1 5
# 2 NA NA
# 3 3 NaN
识别所有列中包含NA或NaN的行。
ind <- rowSums(is.na(my.df)) == ncol(my.df)
示例数据
my.df <- data.frame(a=c(1, NA, 3), b=c(NA, NA, NaN))
my.df
# a b
# 1 1 NA
# 2 NA NA
# 3 3 NaN
识别所有行中具有NA或NaN的列。
ind <- colSums(is.na(my.df)) == nrow(my.df)
ind
# a b
# FALSE TRUE
# to get the column names
names(my.df)[ind]
仅限特定列(根据OP请求):
sum(is.na(my.df[,'b'])) == nrow(my.df)
感谢Roland!
# alternate or best option
all(is.na(my.df[,'b']))
答案 2 :(得分:2)
Uncaught TypeError: Cannot read property 'setState' of undefined
和this
的列, constructor(props) {
super(props);
this.state = {
postBody : "",
location: "main"
};
this.onSubmit = this.onSubmit.bind(this);
this.onChange = this.onChange.bind(this);
}
onChange(e) {
this.setState({
[e.target.name] : e.target.value
});
}
onSubmit(e) {
e.preventDefault();
this.props.createPostRequest(this.state).then(
() => {
this.setState = ({
postBody : ""
});
}
)
}
render() {
return (
<div className="create-post-inner col-md-12">
<form id="createPost" onSubmit={ this.onSubmit } >
<textarea value={this.state.postBody} className="form-control postInput" name="postBody" onChange={ this.onChange } >
</textarea>
<span>The value of the state is {this.state.postBody}</span>
<input type="submit" className="submit btn btn-primary" />
</form>
</div>
);
}
将返回apply(df, 2, function(x) all(is.nan(x)))
。
要获取全部为TRUE
的列的名称,您可以执行以下操作:
NaN
要获取列的索引,您可以执行以下操作:
FALSE