将NA替换为R中同一列中的另一个值

时间:2017-06-09 14:26:31

标签: r

我正在尝试使用dplyr包替换NA在同一列中的进程值,但我没有得到结果。下面是我正在使用的脚本。

MacBook-Pro:Downloads Usesr$ particle flash --usb firmware.bin 
Found DFU device 2b04:d006
spawning dfu-util -d 2b04:d006 -a 0 -i 0 -s 0x080A0000:leave -D firmware.bin
dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

dfu-util: Invalid DFU suffix signature
dfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
Opening DFU capable USB device...
ID 2b04:d006
Run-time device DFU version 011a
Claiming USB DFU Interface...
Setting Alternate Setting #0 ...
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 011a
Device returned transfer size 4096
DfuSe interface name: "Internal Flash   "
Downloading to address = 0x080a0000, size = 5224
Download    [=========================] 100%         5224 bytes
Download done.
File downloaded successfully
dfu-util: Error during download get_status

Error writing firmware...dfu-util: Invalid DFU suffix signature
dfu-util: A valid DFU suffix will be required in a future dfu-util release!!!

dfu-util: Error during download get_status

另请参阅下面我在上面脚本中使用的数据文件。任何人都可以帮助我知道为什么NA​​在使用上述语法时没有被替换。脚本不会抛出错误

data <- read.csv("data.csv",header=T,na.strings=c("","NULL"))

data$ID <- paste(data$ID1, data$ID2, sep='_')

data$End.Date <- as.Date(data$End.Date, "%d-%b-%y")

data1 <- data[order(data$ID, data$End.Date),]

library(tidyr)
library(dplyr)

data1 %>% 
  group_by(ID) %>% 
  fill(Start.date, .direction = 'up') %>% 
  fill(Start.date, .direction = 'down')

1 个答案:

答案 0 :(得分:1)

动物园图书馆有一个很棒的功能叫做na.locf()

library(zoo)
library(dplyr)

data <- read.csv("data.csv",header=T,na.strings=c("","NULL"))

data$ID <- paste(data$ID1, data$ID2, sep='_')

data$End.Date <- as.Date(data$End.Date, "%d-%b-%y")

data1 <- data[order(data$ID, data$End.Date),]

data1 <- data1 %>% 
         group_by(ID) %>% 
         mutate(Start.Date = ifelse(is.na(Start.Date),na.locf(Start.Date, na.rm = FALSE), Start.Date))