na

One function to detect NaN, NA, Inf, -Inf, etc.?

拟墨画扇 提交于 2019-11-27 11:26:21
Is there a single function in R that determines if a value is NA , NaN , Inf , -Inf , or otherwise not a well-formed number? You want is.finite > is.finite(NA) [1] FALSE > is.finite(NaN) [1] FALSE > is.finite(Inf) [1] FALSE > is.finite(1L) [1] TRUE > is.finite(1.0) [1] TRUE > is.finite("A") [1] FALSE > is.finite(pi) [1] TRUE > is.finite(1+0i) [1] TRUE 来源: https://stackoverflow.com/questions/7518245/one-function-to-detect-nan-na-inf-inf-etc

Select NA in a data.table in R

£可爱£侵袭症+ 提交于 2019-11-27 08:02:29
How do I select all the rows that have a missing value in the primary key in a data table. DT = data.table(x=rep(c("a","b",NA),each=3), y=c(1,3,6), v=1:9) setkey(DT,x) Selecting for a particular value is easy DT["a",] Selecting for the missing values seems to require a vector search. One cannot use binary search. Am I correct? DT[NA,]# does not work DT[is.na(x),] #does work Fortunately, DT[is.na(x),] is nearly as fast as (e.g.) DT["a",] , so in practice, this may not really matter much: library(data.table) library(rbenchmark) DT = data.table(x=rep(c("a","b",NA),each=3e6), y=c(1,3,6), v=1:9)

Function to change blanks to NA

巧了我就是萌 提交于 2019-11-27 07:44:23
问题 I'm trying to write a function that turns empty strings into NA. A summary of one of my column looks like this: a b 12 210 468 I'd like to change the 12 empty values to NA. I also have a few other factor columns for which I'd like to change empty values to NA, so I borrowed some stuff from here and there to come up with this: # change nulls to NAs nullToNA <- function(df){ # split df into numeric & non-numeric functions a<-df[,sapply(df, is.numeric), drop = FALSE] b<-df[,sapply(df, Negate(is

Creating (and Accessing) a Sparse Matrix with NA default entries

こ雲淡風輕ζ 提交于 2019-11-27 07:40:57
After learning about the options for working with sparse matrices in R , I want to use the Matrix package to create a sparse matrix from the following data frame and have all other elements be NA . s r d 1 1089 3772 1 2 1109 190 1 3 1109 2460 1 4 1109 3071 2 5 1109 3618 1 6 1109 38 7 I know I can create a sparse matrix with the following, accessing elements as usual: > library(Matrix) > Y <- sparseMatrix(s,r,x=d) > Y[1089,3772] [1] 1 > Y[1,1] [1] 0 but if I want to have the default value to be NA, I tried the following: M <- Matrix(NA,max(s),max(r),sparse=TRUE) for (i in 1:nrow(X)) M[s[i],r[i]

R Change NA values

拜拜、爱过 提交于 2019-11-27 06:58:16
问题 My data looks like this: http://imgur.com/8KgvWvP I want to change the values NA to another value for each column. For example in the column that contains NA , Single and Dual , I want to change all the NA to 'Single' . I tried this code: data_price$nbrSims <- ifelse(is.na(data_price$nbrSims), 'Single', data_price$nbrSims) But then my data looks like this, where Dual became 2 and Single 1 . http://imgur.com/TC1bIgw How can I change the NA values, without changing the other values? Thanks in

How to delete columns that contain ONLY NAs?

五迷三道 提交于 2019-11-27 06:35:35
I have a data.frame containing some columns with all NA values, how can I delete them from the data.frame. Can I use the function na.omit(...) specifying some additional arguments? MadScone One way of doing it: df[, colSums(is.na(df)) != nrow(df)] If the count of NAs in a column is equal to the number of rows, it must be entirely NA. Or similarly df[colSums(!is.na(df)) > 0] Here is a dplyr solution: df %>% select_if(~sum(!is.na(.)) > 0) It seeems like you want to remove ONLY columns with ALL NA s, leaving columns with some rows that do have NA s. I would do this (but I am sure there is an

R: replacing NAs in a data.frame with values in the same position in another dataframe

 ̄綄美尐妖づ 提交于 2019-11-27 05:49:31
问题 I have a dataframe with some NA values: dfa <- data.frame(a=c(1,NA,3,4,5,NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA)) dfa I would like to replace the NAs with values in the same position in another dataframe: dfrepair <- data.frame(a=c(2:7),b=c(6:1),c=c(8:3)) dfrepair I tried: dfa1 <- dfa dfa1 <- ifelse(dfa == NA, dfrepair, dfa) dfa1 but this did not work. 回答1: You can do: dfa <- data.frame(a=c(1,NA,3,4,5,NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA)) dfrepair <- data.frame(a=c(2:7),b=c(6:1),c

fill in NA based on the last non-NA value for each group in R [duplicate]

情到浓时终转凉″ 提交于 2019-11-27 03:26:02
问题 This question already has answers here : Replace missing values (NA) with most recent non-NA by group (6 answers) Closed 4 years ago . My question is I have a dataframe m as below y1 =c( rep("A",5),rep("B",5)) y2 = rep(c(1:5),2) y3 = y2 y3[c(2,7,9)]=NA m = data.frame(y1,y2,y3) y1 y2 y3 1 A 1 1 2 A 2 <NA> 3 A 3 3 4 A 4 4 5 A 5 5 6 B 1 1 7 B 2 <NA> 8 B 3 3 9 B 4 <NA> 10 B 5 5 I want to fill in the NA based on the closest non-NA value "in front of" this NA. My output should look like this: y1 y2

How to find the percentage of NAs in a data.frame?

醉酒当歌 提交于 2019-11-27 02:58:14
问题 I am trying to find the percentage of NAs in columns as well as inside the whole dataframe: The first method which I have commented gives me zero and the second method which is not commented gives me a matrix. Not sure what I am missing. Any hint is truly appreciated! cp.2006<-read.csv(file="cp2006.csv",head=TRUE) #countNAs <- function(x) { # sum(is.na(x)) #} #total=0 #for (i in col(cp.2006)) { # total=countNAs(i)+total #} #print(total) count<-apply(cp.2006, 1, function(x) sum(is.na(x))) dims

Omit rows containing specific column of NA

╄→尐↘猪︶ㄣ 提交于 2019-11-27 02:48:01
I want to know how to omit NA values in a data frame, but only in some columns I am interested in. For example, DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22)) but I only want to omit the data where y is NA , therefore the result should be x y z 1 1 0 NA 2 2 10 33 na.omit seems delete all rows contain any NA . Can somebody help me out of this simple question? But if now I change the question like: DF <- data.frame(x = c(1, 2, 3,NA), y = c(1,0, 10, NA), z=c(43,NA, 33, NA)) If I want to omit only x=na or z=na , where can I put the | in function? You could use the complete