2

I have a few R data frames which contain binary data (0,1) to represent incorrect and correct responses to items on specific subscales. Participants were not asked all questions and have NA to signify this missing data. Older participants started with later items and have NA for early items not asked. Also, most participants did not complete the assessment resulting in many NAs at the end of rows. Example rows are as follows:

Row 1 = NA, NA, NA, 1, 1, 0 , 1, 0, 0, 0, NA, NA Row 2 = 1, 1, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA

I want to replace the all the NAs at the beginning of the rows (if they exist) with 1 and the NAs at the end of the rows with 0.

So the above would be Row 1 = 1,1, 1, 1, 1, 0 , 1, 0, 0, 0, 0,0 Row 2 = 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0

I have tried using the following function:

datComp <- function (x){
  xmin <- min(which(!is.na(x)))
  xmax <- max(which(!is.na(x)))
  if (xmin >1){ 
  x[1:xmin-1] <- 1} 
  x[(xmax+1):length(x)] <- 0
  return(x)
  }

but get this error for some data frames:

Error in data.frame(`1` = c(1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0,  : 
  arguments imply differing number of rows: 36, 37

Is there an existing function that does what I want? If not, can anyone help me with simple code that will do this?

4

1 に答える 1

1

これを行う既存の関数はわかりません。これが1つの方法です:

d <- read.csv(text="NA, NA, NA, 1, 1, 0 , 1, 0, 0, 0, NA, NA
1, 1, 0, 0, 0, NA, NA", header=FALSE, strip.white=TRUE, fill=TRUE)
#   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
# 1 NA NA NA  1  1  0  1  0  0   0  NA  NA
# 2  1  1  0  0  0 NA NA NA NA  NA  NA  NA

t( # apply returns its results in column form, so we transpose here
  apply(d, 
    MARGIN=1, # apply over the rows
    FUN=function(row) # for value in row, if NA and index less than min non-NA index, 1, else 0 
          ifelse(is.na(row), 
          ifelse(seq_along(row) < which.min(is.na(row)), 1, 0), 
          row)
  )
)
#      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
# [1,]  1  1  1  1  1  0  1  0  0   0   0   0
# [2,]  1  1  0  0  0  0  0  0  0   0   0   0
于 2012-11-19T18:19:49.270 に答える