na

How does multinom() treat NA values by default?

旧巷老猫 提交于 2019-12-20 07:35:42
问题 When I am running multinom() , say Y ~ X1 + X2 + X3 , if for one particular row X1 is NA (i.e. missing), but Y , X2 and X3 all have a value, would this entire row be thrown out (like it does in SAS)? How are missing values treated in multinom() ? 回答1: Here is a simple example (from ?multinom from the nnet package) to explore the different na.action : > library(nnet) > library(MASS) > example(birthwt) > (bwt.mu <- multinom(low ~ ., bwt)) Intentionally create a NA value: > bwt[1,"age"]<-NA #

How to replace NAs of a variable with values from another dataframe

南楼画角 提交于 2019-12-20 07:07:15
问题 i hope this one isn´t stupid. I have two dataframes with Variables ID and gender/sex. In df1, there are NAs. In df2, the variable is complete. I want to complete the column in df1 with the values from df2. (In df1 the variable is called "gender". In df2 it is called "sex".) Here is what i tried so far: #example-data ID<-seq(1,30,by=1) df1<-as.data.frame(ID) df2<-df1 df1$gender<-c(NA,"2","1",NA,"2","2","2","2","2","2",NA,"2","1","1",NA,"2","2","2","2","2","1","2","2",NA,"2","2","2","2","2",NA)

R: Split character column and create two new ones

拈花ヽ惹草 提交于 2019-12-20 04:55:41
问题 R users I have a data frame similar to this: a <- c("John, 3 years") b <- c("Mokobe, 11 years") c <- c("Ivan") df <- rbind(a,b,c) df [,1] a "John, 3 years" b "Mokobe, 11 years" c "Ivan" Which function should I use to split the column after comma to get: df [,1] [,2] John 3 years Mokobe 11 years Ivan NA 回答1: we can do a strsplit by the delimiter , and then rbind the list elements after padding with NA at the end to make length same for each list element lst <- strsplit(df[,1], ", ") do.call

Fill in mean values for NA in every column of a data frame [duplicate]

不问归期 提交于 2019-12-20 04:50:10
问题 This question already has answers here : Replace missing values with column mean (11 answers) Closed 3 years ago . if I have a data frame df df=data.frame(x=1:20,y=c(1:10,rep(NA,10)),z=c(rep(NA,5),1:15)) I know to replace NAs with mean value for a given column is, we can use df[is.na(df$x)]=mean(df$x,na.rm=T) What I am trying to find is a way to use a single command so that it does this for the columns at once instead of repeating it for every column. Suspecting, I need to use sapply and

R: Replace elements with NA in a matrix in corresponding positions to NA's in another matrix

三世轮回 提交于 2019-12-20 04:19:01
问题 I have a large matrix, z, that I removed all values >3 and replaced with NA using: z[z>3]<-NA I have another matrix, y , of identical dimensions that I need to replace values with NA in positions corresponding to the locations where the elements were replaced in element z. That is, if z[3,12] was >3 and replaced with NA, I need y[3,12] to be replaced with NA too. They have the same row names if that helps. 回答1: Just use is.na on the first matrix to select the values to replace in the second

Column name of last non-NA row per row; using tidyverse solution?

▼魔方 西西 提交于 2019-12-20 03:43:21
问题 Brief Dataset description: I have survey data generated from Qualtrics, which I've imported into R as a tibble. Each column corresponds to a survey question, and I've preserved the original column order (to correspond with the order of the questions in the survey). Problem in plain language: Due to normal participant attrition, not all participants completed all of the questions in the survey. I want to know how far each participant got in the survey, and the last question they each answered

Replace the NA value of a cell by the value of another column in the same dataframe

巧了我就是萌 提交于 2019-12-20 02:32:17
问题 I have a problem which seems to me quite simple but I don't manage to solve it by myself. I've searched for the solution on StackOverflow, I guess it has already been solved by someone but I haven't found it yet. I have a data frame based upon the merger of 5 data frames, which looks like that : id | mag1 | mag2 | mag3 1 | name | name | name 2 | NA | NA | name 3 | NA | name | NA With mag2 and mag3 there always is a name which is filled (there is no row with an NA in mag1, mag2 and mag3). I

Identify NA's in sequence row-wise

故事扮演 提交于 2019-12-19 09:49:50
问题 I want to fill NA values in a sequence, which is row-wise, based on a condition. Please see example below. ID | Observation 1 | Observation 2 | Observation 3 | Observation 4 | Observation 5 A NA 0 1 NA NA The condition is: all NA values before !NA values in the sequence should be left as NA; but all NAs after !NA values in the sequence should be tagged ("remove") In the example above, NA value in Observation 1 should remain NA. However, the NA values in Observations 4 and 5 should be changed

Quick replace of NA - an error or warning

拜拜、爱过 提交于 2019-12-19 08:05:11
问题 I have a big data.frame called "mat" of 49952 obs. of 7597 variables and I'm trying to replace NAs with zeros. Here is and example how my data.frame looks like: A B C E F D Q Z . . . 1 1 1 0 NA NA 0 NA NA 2 0 0 1 NA NA 0 NA NA 3 0 0 0 NA NA 1 NA NA 4 NA NA NA NA NA NA NA NA 5 0 1 0 1 NA 0 NA NA 6 1 1 1 0 NA 0 NA NA 7 0 0 1 0 NA 1 NA NA . . . I need realy fast tool to replace them. The result should look like: A B C E F D Q Z . . . 1 1 1 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 3 0 0 0 0 0 1 0 0 4 0 0 0

Quick replace of NA - an error or warning

给你一囗甜甜゛ 提交于 2019-12-19 08:05:07
问题 I have a big data.frame called "mat" of 49952 obs. of 7597 variables and I'm trying to replace NAs with zeros. Here is and example how my data.frame looks like: A B C E F D Q Z . . . 1 1 1 0 NA NA 0 NA NA 2 0 0 1 NA NA 0 NA NA 3 0 0 0 NA NA 1 NA NA 4 NA NA NA NA NA NA NA NA 5 0 1 0 1 NA 0 NA NA 6 1 1 1 0 NA 0 NA NA 7 0 0 1 0 NA 1 NA NA . . . I need realy fast tool to replace them. The result should look like: A B C E F D Q Z . . . 1 1 1 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 3 0 0 0 0 0 1 0 0 4 0 0 0