繁体   English   中英

删除R中数据框列中的所有NA向量

[英]Removing all NA vectors in Data frame Column in R

我有一个数据框,其中包含一列“sfc_point”类。 该列由具有向量 c(NA,NA) 的许多行组成。 是否有删除向量并将其替换为 NA 值的功能? 尝试了以下代码,但它似乎不起作用。

clean_data$location.x[!is.na(clean_data$location.x)]

dput(clean_data[1:10, c("location.x", "location.y")])

structure(list(location.x = structure(list(structure(c(NA_real_, 
NA_real_), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
NA_real_), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
NA_real_), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
NA_real_), class = c("XY", "POINT", "sfg")), structure(c(-4131.41409222454, 
7236.52563322564), class = c("XY", "POINT", "sfg")), structure(c(-1623.07405914413, 
3872.0300113645), class = c("XY", "POINT", "sfg")), structure(c(413.840000705876, 
1194.12869422895), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
NA_real_), class = c("XY", "POINT", "sfg")), structure(c(-3136.12789580931, 
6671.91746186715), class = c("XY", "POINT", "sfg")), structure(c(-508.126910446347, 
6307.58442144702), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", 
"sfc"), precision = 0, bbox = structure(c(xmin = -4131.41409222454, 
ymin = 1194.12869422895, xmax = 413.840000705876, ymax = 7236.52563322564
), class = "bbox"), crs = structure(list(input = NA_character_, 
    wkt = NA_character_), class = "crs"), n_empty = 5L), location.y = structure(list(
    structure(c(-3209.73813918762, 6487.6576880651), class = c("XY", 
    "POINT", "sfg")), structure(c(-3209.73813918762, 6487.6576880651
    ), class = c("XY", "POINT", "sfg")), structure(c(-1702.53781555412, 
    3865.47709084848), class = c("XY", "POINT", "sfg")), structure(c(-3209.73813918762, 
    6487.6576880651), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
    NA_real_), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
    NA_real_), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
    NA_real_), class = c("XY", "POINT", "sfg")), structure(c(-1608.76641144993, 
    3886.49247849546), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
    NA_real_), class = c("XY", "POINT", "sfg")), structure(c(NA_real_, 
    NA_real_), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", 
"sfc"), precision = 0, bbox = structure(c(xmin = -3209.73813918762, 
ymin = 3865.47709084848, xmax = -1608.76641144993, ymax = 6487.6576880651
), class = "bbox"), crs = structure(list(input = NA_character_, 
    wkt = NA_character_), class = "crs"), n_empty = 5L)), row.names = c(NA, 
10L), class = "data.frame")


这些列都是list列,因此我们需要遍历list并应用is.na

lapply(clean_data$location.x, \(x) x[!is.na(x)])

如果我们想对list元素进行子集化,那么

> Filter(length, lapply(clean_data$location.x, \(x) x[!is.na(x)]))
[[1]]
[1] -4131.414  7236.526

[[2]]
[1] -1623.074  3872.030

[[3]]
[1]  413.840 1194.129

[[4]]
[1] -3136.128  6671.917

[[5]]
[1] -508.1269 6307.5844

另一种选择是rbind list元素,然后删除NA

m1 <- do.call(rbind, clean_data$location.x)
m1
            [,1]     [,2]
 [1,]         NA       NA
 [2,]         NA       NA
 [3,]         NA       NA
 [4,]         NA       NA
 [5,] -4131.4141 7236.526
 [6,] -1623.0741 3872.030
 [7,]   413.8400 1194.129
 [8,]         NA       NA
 [9,] -3136.1279 6671.917
[10,]  -508.1269 6307.584
na.omit(m1)
          [,1]     [,2]
[1,] -4131.4141 7236.526
[2,] -1623.0741 3872.030
[3,]   413.8400 1194.129
[4,] -3136.1279 6671.917
[5,]  -508.1269 6307.584

如果我们想coalesce

library(purrr)
library(dplyr)
map2_dfr(clean_data$location.x, clean_data$location.y, 
  ~ coalesce(.x, .y) %>% 
    as.data.frame.list %>% 
    setNames(c('a', 'b')))
          a        b
1  -3209.7381 6487.658
2  -3209.7381 6487.658
3  -1702.5378 3865.477
4  -3209.7381 6487.658
5  -4131.4141 7236.526
6  -1623.0741 3872.030
7    413.8400 1194.129
8  -1608.7664 3886.492
9  -3136.1279 6671.917
10  -508.1269 6307.584

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM