na

Why does dplyr's filter drop NA values from a factor variable?

被刻印的时光 ゝ 提交于 2019-11-26 21:13:52
问题 When I use filter from the dplyr package to drop a level of a factor variable, filter also drops the NA values. Here's an example: library(dplyr) set.seed(919) (dat <- data.frame(var1 = factor(sample(c(1:3, NA), size = 10, replace = T)))) # var1 # 1 <NA> # 2 3 # 3 3 # 4 1 # 5 1 # 6 <NA> # 7 2 # 8 2 # 9 <NA> # 10 1 filter(dat, var1 != 1) # var1 # 1 3 # 2 3 # 3 2 # 4 2 This does not seem ideal -- I only wanted to drop rows where var1 == 1 . It looks like this is occurring because any comparison

Dealing with TRUE, FALSE, NA and NaN

不打扰是莪最后的温柔 提交于 2019-11-26 20:24:41
Here is a vector a <- c(TRUE, FALSE, FALSE, NA, FALSE, TRUE, NA, FALSE, TRUE) I'd like a simple function that returns TRUE everytime there is a TRUE in "a", and FALSE everytime there is a FALSE or a NA in "a". The three following things do not work a == TRUE identical(TRUE, a) isTRUE(a) Here is a solution a[-which(is.na(a))] but it doesn't seem to be a straightforward and easy solution Is there another solution ? Here are some functions (and operators) I know: identical() isTRUE() is.na() na.rm() & | ! What are the other functions (operators, tips, whatever,...) that are useful to deal with

Subsetting R data frame results in mysterious NA rows

两盒软妹~` 提交于 2019-11-26 18:41:38
I've been encountering what I think is a bug. It's not a big deal, but I'm curious if anyone else has seen this. Unfortunately, my data is confidential, so I have to make up an example, and it's not going to be very helpful. When subsetting my data, I occassionally get mysterious NA rows that aren't in my original data frame. Even the rownames are NA. EG: example <- data.frame("var1"=c("A", "B", "A"), "var2"=c("X", "Y", "Z")) example var1 var2 1 A X 2 B Y 3 A Z then I run: example[example$var1=="A",] var1 var2 1 A X 3 A Z NA<NA> <NA> Of course, the example above does not actually give you this

Remove columns from dataframe where some of values are NA

三世轮回 提交于 2019-11-26 18:22:07
问题 I have a dataframe where some of the values are NA. I would like to remove these columns. My data.frame looks like this v1 v2 1 1 NA 2 1 1 3 2 2 4 1 1 5 2 2 6 1 NA I tried to estimate the col mean and select the column means !=NA. I tried this statement, it does not work. data=subset(Itun, select=c(is.na(colMeans(Itun)))) I got an error, error : 'x' must be an array of at least two dimensions Can anyone give me some help? 回答1: The data: Itun <- data.frame(v1 = c(1,1,2,1,2,1), v2 = c(NA, 1, 2,

How to replace NA with mean by subset in R (impute with plyr?)

南笙酒味 提交于 2019-11-26 17:21:14
I have a dataframe with the lengths and widths of various arthropods from the guts of salamanders. Because some guts had thousands of certain prey items, I only measured a subset of each prey type. I now want to replace each unmeasured individual with the mean length and width for that prey. I want to keep the dataframe and just add imputed columns (length2, width2). The main reason is that each row also has columns with data on the date and location the salamander was collected. I could fill in the NA with a random selection of the measured individuals but for the sake of argument let's

Aligning Data frame with missing values

陌路散爱 提交于 2019-11-26 17:21:02
问题 I'm using a data frame with many NA values. While I'm able to create a linear model, I am subsequently unable to line the fitted values of the model up with the original data due to the missing values and lack of indicator column. Here's a reproducible example: library(MASS) dat <- Aids2 # Add NA's dat[floor(runif(100, min = 1, max = nrow(dat))),3] <- NA # Create a model model <- lm(death ~ diag + age, data = dat) # Different Values length(fitted.values(model)) # 2745 nrow(dat) # 2843 回答1:

Handle Continous Missing values in time-series data

半城伤御伤魂 提交于 2019-11-26 16:48:29
问题 I have a time-series data as shown below. 2015-04-26 23:00:00 5704.27388916015661380 2015-04-27 00:00:00 4470.30868326822928793 2015-04-27 01:00:00 4552.57241617838553793 2015-04-27 02:00:00 4570.22250032825650123 2015-04-27 03:00:00 NA 2015-04-27 04:00:00 NA 2015-04-27 05:00:00 NA 2015-04-27 06:00:00 12697.37724086216439900 2015-04-27 07:00:00 5538.71119009653739340 2015-04-27 08:00:00 81.95060647328695325 2015-04-27 09:00:00 8550.65816895300667966 2015-04-27 10:00:00 2925.76573206583680076

Replace <NA> in a factor column

假如想象 提交于 2019-11-26 16:34:31
问题 I want to replace <NA> values in a factors column with a valid value. But I can not find a way. This example is only for demonstration. The original data comes from a foreign csv file I have to deal with. df <- data.frame(a=sample(0:10, size=10, replace=TRUE), b=sample(20:30, size=10, replace=TRUE)) df[df$a==0,'a'] <- NA df$a <- as.factor(df$a) Could look like this a b 1 1 29 2 2 23 3 3 23 4 3 22 5 4 28 6 <NA> 24 7 2 21 8 4 25 9 <NA> 29 10 3 24 Now I want to replace the <NA> values with a

Change the Blank Cells to “NA”

人盡茶涼 提交于 2019-11-26 15:50:29
Here's the link of my data. My target is to assign "NA" to all blank cells irrespective of categorical or numerical values. I am using na.strings="" . But it's not assigning NA to all blank cells. ## reading the data dat <- read.csv("data2.csv") head(dat) mon hr acc alc sex spd axles door reg cond1 drug1 1 8 21 No Control TRUE F 0 2 2 Physical Impairment (Eyes, Ear, Limb) A 2 7 20 No Control FALSE M 900 2 2 Inattentive D 3 3 9 No Control FALSE F 100 2 2 2004 Normal D 4 1 15 No Control FALSE M 0 2 2 Physical Impairment (Eyes, Ear, Limb) D 5 4 21 No Control FALSE 25 NA NA D 6 4 20 No Control NA

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

痴心易碎 提交于 2019-11-26 15:35:33
问题 Is there a single function in R that determines if a value is NA , NaN , Inf , -Inf , or otherwise not a well-formed number? 回答1: 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