I'm hoping and fearing this just has a one line answer, and am embarrassed about having to ask it:
我希望并担心这个问题只有一句话回答,不得不问这个问题让我很尴尬:
As a result of casting a dataframe using reshape2, I have a large dataframe whose first lines are as follows:
由于使用reshape2对dataframe进行了强制转换,我有一个大的dataframe,其第一行如下:
ukgridcode x y year pm10 pm2.5 no2 nox co so2 ozone benzene
1 56361 460500 1218500 2001 8.583796 NaN 2.375508 3.031918 NaN NaN NaN NaN
2 57051 460500 1217500 2001 8.584764 NaN 2.368387 3.022830 NaN NaN NaN NaN
3 57052 461500 1217500 2001 8.587392 NaN 2.383438 3.042039 NaN NaN NaN NaN
4 57054 463500 1217500 2001 8.590163 NaN 2.455386 3.133869 NaN NaN NaN NaN
5 57741 460500 1216500 2001 8.588822 NaN 2.375839 3.032340 NaN NaN NaN NaN
6 57742 461500 1216500 2001 8.592360 NaN 2.394949 3.056732 NaN NaN NaN NaN
I would like to turn each of the NaNs into NAs. So far I've tried:
我想把每个NaNs转换成NAs。到目前为止我已经试过:
d2 <- d[is.nan(d)] <- NA
d2 <- d[!is.finite(d)] <- NA
d2 <- apply(d, 2, function (x) x[is.nan(x)] <- NA) # also tried row-wise (1)
But so far no success.
但到目前为止还没有成功。
Solutions gratefully received.
欣然接受的解决方案。
2 个解决方案
#1
4
Try
试一试
d[sapply(d, is.nan)] <- NA
# ukgridcode x y year pm10 pm2.5 no2 nox co so2 ozone benzene
# 1 56361 460500 1218500 2001 8.583796 NA 2.375508 3.031918 NA NA NA NA
# 2 57051 460500 1217500 2001 8.584764 NA 2.368387 3.022830 NA NA NA NA
# 3 57052 461500 1217500 2001 8.587392 NA 2.383438 3.042039 NA NA NA NA
# 4 57054 463500 1217500 2001 8.590163 NA 2.455386 3.133869 NA NA NA NA
# 5 57741 460500 1216500 2001 8.588822 NA 2.375839 3.032340 NA NA NA NA
# 6 57742 461500 1216500 2001 8.592360 NA 2.394949 3.056732 NA NA NA NA
Or using data.table
或者使用data.table
library(data.table)
setDT(d)[, lapply(.SD, function(x) ifelse(is.nan(x), NA, x))]
Or just look at @Rolands comment
或者看看@Rolands的评论
#2
0
You can try:
你可以尝试:
is.nan(d$pm2.5)
#[1] TRUE TRUE TRUE TRUE TRUE TRUE
d[d=="NaN"] <- NA
d
# ukgridcode x y year pm10 pm2.5 no2 nox co so2 ozone
#1 56361 460500 1218500 2001 8.583796 NA 2.375508 3.031918 NA NA NA
#2 57051 460500 1217500 2001 8.584764 NA 2.368387 3.022830 NA NA NA
#3 57052 461500 1217500 2001 8.587392 NA 2.383438 3.042039 NA NA NA
#4 57054 463500 1217500 2001 8.590163 NA 2.455386 3.133869 NA NA NA
#5 57741 460500 1216500 2001 8.588822 NA 2.375839 3.032340 NA NA NA
#6 57742 461500 1216500 2001 8.592360 NA 2.394949 3.056732 NA NA NA
# benzene
#1 NA
#2 NA
#3 NA
#4 NA
#5 NA
#6 NA
#1
4
Try
试一试
d[sapply(d, is.nan)] <- NA
# ukgridcode x y year pm10 pm2.5 no2 nox co so2 ozone benzene
# 1 56361 460500 1218500 2001 8.583796 NA 2.375508 3.031918 NA NA NA NA
# 2 57051 460500 1217500 2001 8.584764 NA 2.368387 3.022830 NA NA NA NA
# 3 57052 461500 1217500 2001 8.587392 NA 2.383438 3.042039 NA NA NA NA
# 4 57054 463500 1217500 2001 8.590163 NA 2.455386 3.133869 NA NA NA NA
# 5 57741 460500 1216500 2001 8.588822 NA 2.375839 3.032340 NA NA NA NA
# 6 57742 461500 1216500 2001 8.592360 NA 2.394949 3.056732 NA NA NA NA
Or using data.table
或者使用data.table
library(data.table)
setDT(d)[, lapply(.SD, function(x) ifelse(is.nan(x), NA, x))]
Or just look at @Rolands comment
或者看看@Rolands的评论
#2
0
You can try:
你可以尝试:
is.nan(d$pm2.5)
#[1] TRUE TRUE TRUE TRUE TRUE TRUE
d[d=="NaN"] <- NA
d
# ukgridcode x y year pm10 pm2.5 no2 nox co so2 ozone
#1 56361 460500 1218500 2001 8.583796 NA 2.375508 3.031918 NA NA NA
#2 57051 460500 1217500 2001 8.584764 NA 2.368387 3.022830 NA NA NA
#3 57052 461500 1217500 2001 8.587392 NA 2.383438 3.042039 NA NA NA
#4 57054 463500 1217500 2001 8.590163 NA 2.455386 3.133869 NA NA NA
#5 57741 460500 1216500 2001 8.588822 NA 2.375839 3.032340 NA NA NA
#6 57742 461500 1216500 2001 8.592360 NA 2.394949 3.056732 NA NA NA
# benzene
#1 NA
#2 NA
#3 NA
#4 NA
#5 NA
#6 NA