Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

r - Is there an efficient method to check for 8 successive elements that are not NA (i.e. is.na()==FALSE) in each column of a large dataset?

I have 200 datasets with size of 5120*732 and I saved them in a List-type variable "data". Some of the elements are NA.

For example,the 13th and 14th columns are:


... 13 14 ...

... 13 14 ...

... NA 14 ...

... NA 14 ...

... 13 14 ...

... 13 14 ...

... 13 14 ...

... 13 14 ...

... 13 14 ...

... 13 14 ...

Now in each column after the 12th column(i.e. from the 13th column), once there're >= N1 (N1 = 8) successive elements that are not NA (i.e. is.na()==FALSE), I would like to prefix all of them with 'D'.

Like:


... 13 D14 ...

... 13 D14 ...

... NA D14 ...

... NA D14 ...

... 13 D14 ...

... 13 D14 ...

... 13 D14 ...

... 13 D14 ...

... 13 D14 ...

... 13 D14 ...

Here's my code:

for (i in 1:length(data)) { #traverse List"data"
 for (j in 1:dim(data[[i]])[1]) { #traverse each row
   for (k in 13:dim(data[[i]])[2]) { #traverse each column
      #Here N1 = 8
     if((j+N1-1)<=dim(data[[i]])[1] && sum(is.na(data[[i]][j:(j+N1-1),k])==FALSE)==N1) {
       data[[i]][j:(j+N1-1),k] <- paste('D', data[[i]][j:(j+N1-1),k], sep = '')
     }
   } 
 }
}

It works on small datasets but it's way too slow for large datasets.

Is there an efficient method to check for 8 successive elements that are not NA (i.e. is.na()==FALSE) in each column of a large dataset?

Thank you in advance!

I tried the apply() function but I don't know how to complete it.

for (i in 1:length(data)) {  
  apply(data[[i]][,13:732], MARGIN = c(1,2),function(x){

  })
}

@RonakShah, here's an example(N1 = 3, 10 rows * 10 columns), please let me know if you need more information.

Original data: ====================== > Expected Outcome:

1  2  3  4  5  6  7  8  9  10      1  2  D3  D4  5  6   D7  D8  D9  D10  

NA 2  3  4  5  6  7  8  9  10      NA 2  D3  D4  5  6   D7  D8  D9  D10  

1  NA 3  4  NA NA 7  8  9  10      D1 NA D3  D4  NA NA  D7  D8  D9  D10  

1  NA 3  4  5  6  7  8  9  10      D1 NA D3  D4  5  6   D7  D8  D9  D10  

1  NA 3  4  NA NA 7  8  NA NA ===> D1 NA D3  D4  NA NA  D7  D8  NA  NA

NA NA 3  4  5  6  7  8  9  NA      NA NA D3  D4  5  D6  D7  D8  D9  NA

NA NA 3  4  5  6  7  8  9  NA      NA NA D3  D4  5  D6  D7  D8  D9  NA

1  NA 3  4  NA 6  7  8  9  NA      1  NA D3  D4  NA D6  D7  D8  D9  NA

1  NA 3  4  NA 6  7  8  9  NA      1  NA D3  D4  NA D6  D7  D8  D9  NA

NA NA 3  4  5  6  7  8  9  NA      NA NA D3  D4  5  D6  D7  D8  D9  NA

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code does what the question asks for.
The function that does all the work is append_one. It

  1. Creates a vector Y repeating the prefix length(x) times.
  2. Gets the runs of vector y.
  3. Cleans the runs' values to the empty string "" if the runs' lengths are less than N.
  4. Reverses the run-length encoding.
  5. Pastes this vector of prefixes with the input vector x.

Then function append_all calls this function on every column of the input data frame.

append_one <- function(x, N, pref = "D"){
  y <- rep(pref, length(x))
  is.na(y) <- is.na(x)
  r <- rle(y)
  r$values[r$lengths < N] <- ""
  y <- inverse.rle(r)
  paste0(y, x)
}

append_all <- function(X, n, pref = "D"){
  Y <- X
  Y [] <- lapply(Y, append_one, N = n, pref = pref)
  Y
}

N1 <- 3
append_all(df1, N1)

Data.

Original data set, posted in the question.

df <- data.frame(c(1,NA,1,1,1),
                  c(2,2,NA,NA,NA),
                  c(3,3,3,3,NA),
                  c(4,4,4,4,4),
                  c(5,NA,5,NA,5))

New data set and corresponding output, posted in a comment.

df1 <- data.frame(c(1.0,NA,1.1,1.2,1.3),
                  c(2.0,2.1,NA,NA,NA),
                  c(3.0,3.1,3.2,3.3,NA),
                  c(4.0,4.1,4.2,4.3,4.4),
                  c(5.0,NA,5.1,NA,5.2))

df2 <- data.frame(c(1.0,NA,'D1.1','D1.2','D1.3'),
                  c(2.0,2.1,NA,NA,NA),
                  c('D3.0','D3.1','D3.2','D3.3',NA),
                  c('D4.0','D4.1','D4.2','D4.3','D4.4'),
                  c(5.0,NA,5.1,NA,5.2))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...