na

Replace NA values from a column with 0 in data frame R [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-18 10:34:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Set NA to 0 in R I have a data.frame with a column having NA values. I want to replace NA with 0 or any other value. I have tried a lot of threads and methods but it did not give me the result. I have tried the below methods. a$x[a$x==NA]<-0; a[,c("x")]<-apply(a[,c("x")],1,function(z){replace(z, is.na(z), 0)}); a$x[is.na(a$x),]<-0; None of the above methods replaced NA with 0 in column x for data.frame a . Why?

Replacing NAs in R with nearest value

柔情痞子 提交于 2019-12-18 10:33:28
问题 I'm looking for something similar to na.locf() in the zoo package, but instead of always using the previous non- NA value I'd like to use the nearest non- NA value. Some example data: dat <- c(1, 3, NA, NA, 5, 7) Replacing NA with na.locf (3 is carried forward): library(zoo) na.locf(dat) # 1 3 3 3 5 7 and na.locf with fromLast set to TRUE (5 is carried backwards): na.locf(dat, fromLast = TRUE) # 1 3 5 5 5 7 But I wish the nearest non- NA value to be used. In my example this means that the 3

Fastest way to detect if vector has at least 1 NA?

♀尐吖头ヾ 提交于 2019-12-18 10:20:32
问题 What is the fastest way to detect if a vector has at least 1 NA in R? I've been using: sum( is.na( data ) ) > 0 But that requires examining each element, coercion, and the sum function. 回答1: As of R 3.1.0 anyNA() is the way to do this. On atomic vectors this will stop after the first NA instead of going through the entire vector as would be the case with any(is.na()) . Additionally, this avoids creating an intermediate logical vector with is.na that is immediately discarded. Borrowing Joran's

Correct syntax for mutate_if

天涯浪子 提交于 2019-12-18 10:17:16
问题 I would like to replace NA values with zeros via mutate_if in dplyr . The syntax below: set.seed(1) mtcars[sample(1:dim(mtcars)[1], 5), sample(1:dim(mtcars)[2], 5)] <- NA require(dplyr) mtcars %>% mutate_if(is.na,0) mtcars %>% mutate_if(is.na, funs(. = 0)) returns error: Error in vapply(tbl, p, logical(1), ...) : values must be length 1, but FUN(X[[1]]) result is length 32 What's the correct syntax for this operation? 回答1: I learned this trick from the purrr tutorial, and it also works in

max.col with NA removal

两盒软妹~` 提交于 2019-12-18 08:10:26
问题 I'm looking to find the columns of matrix row-maxima while ignoring NAs. E.g., set.seed(1) a <- matrix(runif(15), ncol=3) a[a<.3] <- NA a[5,] <- NA That is: > a [,1] [,2] [,3] [1,] NA 0.898 NA [2,] 0.372 0.945 NA [3,] 0.573 0.661 0.687 [4,] 0.908 0.629 0.384 [5,] NA NA NA The row maxima, ignoring NAs, can be obtained using max : > apply(a, 1, max, na.rm=T) [1] 0.898 0.945 0.687 0.908 -Inf I'm looking for the column positions of these maxima, but max.col only works for rows without any NAs. >

Dealing with NAs when calculating mean (summarize_each) on group_by

六眼飞鱼酱① 提交于 2019-12-18 04:44:08
问题 I have a data frame md: md <- data.frame(x = c(3,5,4,5,3,5), y = c(5,5,5,4,4,1), z = c(1,3,4,3,5,5), device1 = c("c","a","a","b","c","c"), device2 = c("B","A","A","A","B","B")) md[2,3] <- NA md[4,1] <- NA md I want to calculate means by device1 / device2 combinations using dplyr: library(dplyr) md %>% group_by(device1, device2) %>% summarise_each(funs(mean)) However, I am getting some NAs. I want the NAs to be ignored (na.rm = TRUE) - I tried, but the function doesn't want to accept this

Test for NA and select values based on result

こ雲淡風輕ζ 提交于 2019-12-18 03:03:31
问题 My question is rather simple. What I want is if A[i]!=NA , then C[i]=A[i] , if A[i]=NA , then C[i]=B[i] , however, I always get some error messages. Can somebody help me out? A B C NA 82.6 . NA 127.2 . NA 93.6 . NA 105 . NA 104 . NA 90.6 . NA 95.8 . NA 103 . NA 85.4 . NA 81.5 . NA 142.8 . NA 102.3 . NA 104 . NA 103 . NA 94.6 . NA 113.8 . NA 113.5 . NA 74.5 . NA 123.8 . NA 94 . NA 89.8 . NA 74 . NA 104 . NA 100.5 . NA 102.9 . NA 132.5 . NA 91 . NA 92.5 . NA 97 . NA 90 . 54.6 51.7 . NA 61 . NA

model.matrix() with na.action=NULL?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 23:22:27
问题 I have a formula and a data frame, and I want to extract the model.matrix() . However, I need the resulting matrix to include the NAs that were found in the original dataset. If I were to use model.frame() to do this, I would simply pass it na.action=NULL . However, the output I need is of the model.matrix() format. Specifically, I need only the right-hand side variables, I need the output to be a matrix (not a data frame), and I need factors to be converted to a series of dummy variables. I

Removing NA in dplyr pipe [duplicate]

本秂侑毒 提交于 2019-12-17 21:42:25
问题 This question already has answers here : filter for complete cases in data.frame using dplyr (case-wise deletion) (6 answers) Closed 3 years ago . I tried to remove NA's from the subset using dplyr piping. Is my answer an indication of a missed step. I'm trying to learn how to write functions using dplyr: > outcome.df%>% + group_by(Hospital,State)%>% + arrange(desc(HeartAttackDeath,na.rm=TRUE))%>% + head() Source: local data frame [6 x 5] Groups: Hospital, State Hospital State

How to skip a paste() argument when its value is NA in R

折月煮酒 提交于 2019-12-17 19:45:51
问题 I have a data frame with the columns city, state, and country . I want to create a string that concatenates: "City, State, Country". However, one of my cities doesn't have a State (has a NA instead). I want the string for that city to be "City, Country". Here is the code that creates the wrong string: # define City, State, Country city <- c("Austin", "Knoxville", "Salk Lake City", "Prague") state <- c("Texas", "Tennessee", "Utah", NA) country <- c("United States", "United States", "United