na

R data.table multi column recode/sub-assign [duplicate]

被刻印的时光 ゝ 提交于 2019-12-30 22:54:30
问题 This question already has answers here : Fastest way to replace NAs in a large data.table (9 answers) Closed 4 years ago . Let DT be a data.table: DT<-data.table(V1=sample(10), V2=sample(10), ... V9=sample(10),) Is there a better/simpler method to do multicolumn recode/sub-assign like this: DT[V1==1 | V1==7,V1:=NA] DT[V2==1 | V2==7,V2:=NA] DT[V3==1 | V3==7,V3:=NA] DT[V4==1 | V4==7,V4:=NA] DT[V5==1 | V5==7,V5:=NA] DT[V6==1 | V6==7,V6:=NA] DT[V7==1 | V7==7,V7:=NA] DT[V8==1 | V8==7,V8:=NA] DT[V9

R data.table multi column recode/sub-assign [duplicate]

社会主义新天地 提交于 2019-12-30 22:53:35
问题 This question already has answers here : Fastest way to replace NAs in a large data.table (9 answers) Closed 4 years ago . Let DT be a data.table: DT<-data.table(V1=sample(10), V2=sample(10), ... V9=sample(10),) Is there a better/simpler method to do multicolumn recode/sub-assign like this: DT[V1==1 | V1==7,V1:=NA] DT[V2==1 | V2==7,V2:=NA] DT[V3==1 | V3==7,V3:=NA] DT[V4==1 | V4==7,V4:=NA] DT[V5==1 | V5==7,V5:=NA] DT[V6==1 | V6==7,V6:=NA] DT[V7==1 | V7==7,V7:=NA] DT[V8==1 | V8==7,V8:=NA] DT[V9

Replace 0s with NA in tables

点点圈 提交于 2019-12-30 18:00:14
问题 I generally work with dataframes and could easily do this for a data frame but on my current project I have the need to replace all zeros with NAs in a table structure. For the following two tables (one using table and the other using ftable) how could I replace all zero counts with NA? x <- with(mtcars,table(am, gear, cyl, vs)) x2 <- with(mtcars,ftable(am, gear, cyl, vs)) 回答1: This should work: x[x==0] <- NA 来源: https://stackoverflow.com/questions/9822897/replace-0s-with-na-in-tables

Aggregate by NA in R

心不动则不痛 提交于 2019-12-30 09:43:39
问题 Does anybody know how to aggregate by NA in R. If you take the example below a <- matrix(1,5,2) a[1:2,2] <- NA a[3:5,2] <- 2 aggregate(a[,1], by=list(a[,2]), sum) The output is: Group.1 x 2 3 But is there a way to get the output to include NAs in the output like this: Group.1 x 2 3 NA 2 Thanks 回答1: Instead of aggregate() , you may want to consider rowsum() . It is actually designed for this exact operation on matrices and is known to be much faster than aggregate() . We can add NA to the

Dealing with missing values for correlations calculation

走远了吗. 提交于 2019-12-29 19:26:04
问题 I have huge matrix with a lot of missing values. I want to get the correlation between variables. 1. Is the solution cor(na.omit(matrix)) better than below? cor(matrix, use = "pairwise.complete.obs") I already have selected only variables having more than 20% of missing values. 2. Which is the best method to make sense ? 回答1: I would vote for the second option. Sounds like you have a fair amount of missing data and so you would be looking for a sensible multiple imputation strategy to fill in

Dealing with missing values for correlations calculation

天涯浪子 提交于 2019-12-29 19:23:12
问题 I have huge matrix with a lot of missing values. I want to get the correlation between variables. 1. Is the solution cor(na.omit(matrix)) better than below? cor(matrix, use = "pairwise.complete.obs") I already have selected only variables having more than 20% of missing values. 2. Which is the best method to make sense ? 回答1: I would vote for the second option. Sounds like you have a fair amount of missing data and so you would be looking for a sensible multiple imputation strategy to fill in

Filling NA row values with nearest right side row value in R

a 夏天 提交于 2019-12-29 09:17:32
问题 I want to convert the given dataframe from c1 c2 c3 c4 c5 VEG PUFF <NA> 12 <NA> <NA> 78.43 CHICKEN PUFF <NA> 16 <NA> 88.24 <NA> BAKERY Total <NA> <NA> 28 <NA> 84.04 to c1 c2 VEG PUFF 12 78.43 CHICKEN PUFF 16 88.24 BAKERY Total 28 84.04 I tried two methods but i didnt get accurate results it is sometimes taking left side row value step1 <- t(na.locf(t(df), fromLast=T)) step2 <- t(na.locf(t(step1), fromLast=F)) library(dplyr) MyReplace = function(data) {data %>% t %>% na.locf(.,,T) %>% na.locf

Replace NA with Zero in dplyr without using list()

走远了吗. 提交于 2019-12-28 18:24:55
问题 In dplyr I can replace NA with 0 using the following code. The issue is this inserts a list into my data frame which screws up further analysis down the line. I don't even understand lists or atomic vectors or any of that at this point. I just want to pick certain columns, and replace all occurrences of NA with zero. And maintain the columns integer status. library(dplyr) df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"), z = list(1:5, NULL, 10:20)) df df %>% replace_na(list(x = 0, y =

Replacing character values with NA in a data frame

半腔热情 提交于 2019-12-27 10:45:32
问题 I have a data frame containing (in random places) a character value (say "foo" ) that I want to replace with a NA . What's the best way to do so across the whole data frame? 回答1: This: df[ df == "foo" ] <- NA 回答2: One way to nip this in the bud is to convert that character to NA when you read the data in in the first place. df <- read.csv("file.csv", na.strings = c("foo", "bar")) 回答3: Another option is is.na<- : is.na(df) <- df == "foo" Note that its use may seem a bit counter-intuitive, but

strptime returning NA values [closed]

≯℡__Kan透↙ 提交于 2019-12-27 01:21:30
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I'm trying to use strptime to format dates I'm reading in but only get NA values are returned in the output. My raw data is in the format of 1974-01-01, and the length of the dataset is 12049 so the last date is 2006-12-31. The code I use is: Data$date.yyyymmdd <- as.POSIXct(strptime(Data$date.yyyymmdd, format =