na

Rfast hd.eigen() returns NAs but base eigen() does not

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-05 04:12:12
问题 I am having problems with hd.eigen in Rfast . It gives extremely close results to eigen with most data, but sometimes hd.eign returns an empty $vector , NAs, or other undesirable results. For example: > set.seed(123) > bigm <- matrix(rnorm(2000*2000,mean=0,sd = 3), 2000, 2000) > > e3 = eigen(bigm) > length(e3$values) [1] 2000 > length(e3$vectors) [1] 4000000 > sum(is.na(e3$vectors) == TRUE) [1] 0 > sum(is.na(e3$vectors) == FALSE) [1] 4000000 > > e4 = hd.eigen(bigm, vectors = TRUE) > length(e4

add exact proportion of random missing values to data.frame

二次信任 提交于 2020-01-03 14:17:09
问题 I would like to add random NA to a data.frame in R. So far I've looked into these questions: R: Randomly insert NAs into dataframe proportionaly How do I add random NAs into a data frame add random missing values to a complete data frame (in R) Many solutions were provided here, but I couldn't find one that comply with these 5 conditions: Add really random NA, and not the same amount by row or by column Work with every class of variable that one can encounter in a data.frame (numeric,

When subsetting rows with a factor with equal (==), NA's are also included. It doesn't happen with %in%. Is it normal?

笑着哭i 提交于 2020-01-03 11:49:52
问题 Suppose I have a factor A with 3 levels A1, A2, A3 and with NA's. Each appears in 10 cases, so there is a total of 40 cases. If I do subset1 <- df[df$A=="A1",] dim(subset1) # 20, i.e., 10 for A1 and 10 for NA's summary(subset1$A) # both A1 and NA have non-zero counts subset2 <- df[df$A %in% c("A1"),] dim(subset2) # 10, as expected summary(subset2$A) # only A1 has non-zero count And it is the same whether the class of the variable used for subsetting is factor or integer. Is it just how equal

When subsetting rows with a factor with equal (==), NA's are also included. It doesn't happen with %in%. Is it normal?

◇◆丶佛笑我妖孽 提交于 2020-01-03 11:49:04
问题 Suppose I have a factor A with 3 levels A1, A2, A3 and with NA's. Each appears in 10 cases, so there is a total of 40 cases. If I do subset1 <- df[df$A=="A1",] dim(subset1) # 20, i.e., 10 for A1 and 10 for NA's summary(subset1$A) # both A1 and NA have non-zero counts subset2 <- df[df$A %in% c("A1"),] dim(subset2) # 10, as expected summary(subset2$A) # only A1 has non-zero count And it is the same whether the class of the variable used for subsetting is factor or integer. Is it just how equal

Replace na's with value from another df

一笑奈何 提交于 2020-01-02 20:04:52
问题 I have two dataframes below, the first df has ~15k records of number of steps taken by time and date, the second df is the average number of steps per interval time. What I'm trying to do is go through df1 and replace the na values with the avg.steps value from df2, however I've can't seem to figure it out R. What would be the most efficient way to do this? And is there a way to do it using dplyr? df1 looks like this: steps <- c(51, 516, NA, NA, 161, 7) interval <- c(915, 920, 925, 930, 935,

replace duplicate values with NA in time series data using dplyr

蹲街弑〆低调 提交于 2020-01-02 04:34:29
问题 My data seems a bit different than other similar kind of posts. box_num date x y 1-Q 2018-11-18 20.2 8 1-Q 2018-11-25 21.23 7.2 1-Q 2018-12-2 21.23 23 98-L 2018-11-25 0.134 9.3 98-L 2018-12-2 0.134 4 76-GI 2018-12-2 22.734 4.562 76-GI 2018-12-9 28 4.562 Here I would like to replace the repeated values with NA in both x and y columns. The code I have tried using dplyr : (1)df <- df %>% group_by(box_num) %>% arrange(box_num,date) %>% mutate(df$x[duplicated(df$x),] <- NA) It creates a new column

logical(0) in if statement

我与影子孤独终老i 提交于 2020-01-02 03:15:53
问题 This line: which(!is.na(c(NA,NA,NA))) == 0 produces logical(0) While this line if(which(!is.na(c(NA,NA,NA))) == 0){print('TRUE')} generates: Error in if (which(!is.na(c(NA, NA, NA))) == 0) { : argument is of length zero Why there is an error? What is logical(0) 回答1: logical(0) is a vector of class logical with 0 length. You're getting this because your asking which elements of this vector equal 0: > !is.na(c(NA, NA, NA)) [1] FALSE FALSE FALSE > which(!is.na(c(NA, NA, NA))) == 0 logical(0) In

NA in data.table

做~自己de王妃 提交于 2020-01-01 08:44:31
问题 I have a data.table that contains some groups. I operate on each group and some groups return numbers, others return NA . For some reason data.table has trouble putting everything back together. Is this a bug or am I misunderstanding? Here is an example: dtb <- data.table(a=1:10) f <- function(x) {if (x==9) {return(NA)} else { return(x)}} dtb[,f(a),by=a] Error in `[.data.table`(dtb, , f(a), by = a) : columns of j don't evaluate to consistent types for each group: result for group 9 has column

Insert NA values into dataframe blank cells when importing read.csv/read.xlsx

旧街凉风 提交于 2020-01-01 03:23:13
问题 The attached screenshot shows part of a dataframe which I have just imported into R from an excel file. In the cells which are blank, I need to insert 'NA'. How can I insert NA into any cell which is blank (whilst leaving the already populated cells alone)? 回答1: The better question is how can I read it into R so the missing cells will already be NA s. Maybe you used something like this: read.csv(file, header=FALSE, strip.white = TRUE, sep=",") Specify the NA strings like this when you read it

Dropping all left NAs in a dataframe and left shifting the cleaned rows

ε祈祈猫儿з 提交于 2019-12-31 02:39:25
问题 I have the following dataframe dat , which presents a row-specific number of NAs at the beginning of some of its rows: dat <- as.data.frame(rbind(c(NA,NA,1,3,5,NA,NA,NA), c(NA,1:3,6:8,NA), c(1:7,NA))) dat # V1 V2 V3 V4 V5 V6 V7 V8 # NA NA 1 3 5 NA NA NA # NA 1 2 3 6 7 8 NA # 1 NA 2 3 4 5 6 NA My aim is to delete all the NAs at the beginning of each row and to left shift the row values (adding NAs at the end of the shifted rows accordingly, in order to keep their length constant). The