English 中文(简体)
如何计算 NA 行的变化百分比
原标题:How to calculate the percentage of change on rows with NA

当有几名新来港定居人士在整个数据中分布时,我如何发现和计算指标变化的百分比?

让我们假设这是我的数据框架:

     a   b   c   d   e
07  10  NA  NA   8   7
06   8  NA   5  NA   8
05  11  NA   7   6   7
04   5  NA   6   5   9
03  NA  NA  NA  NA  NA
02  NA  NA  NA   3  12
01  NA  NA  10  NA  15

Knowing that each row represents data from a certain year, I want to find the percentage of difference between the lowest row (earliest row) that contains a non-NA number and the uppermost row (latest row) that contains a non-NA number. The result should look like this:

 a     b     c     d     e
 1    NA    -1  1.66 -0.53

The code should be easily replicated since I have hundreds of rows to analyse (eg: it shouldn t involve manually adding each column or row name). Any help will be greatly appreciated!

问题回答

首先发现非 NA s,然后检查任何列中是否有所有 NA ,最后以最低和最高指数进行计算。

sapply(df, (x){ 
  w_case <- which(!is.na(x))
  ifelse(length(w_case) > 0, 
         (x[min(w_case)] - x[max(w_case)]) / x[max(w_case)], 
         NA)})
         a          b          c          d          e 
 1.0000000         NA -0.5000000  1.6666667 -0.5333333

Data

df <- structure(list(a = c(10L, 8L, 11L, 5L, NA, NA, NA), b = c(NA, 
NA, NA, NA, NA, NA, NA), c = c(NA, 5L, 7L, 6L, NA, NA, 10L), 
    d = c(8L, NA, 6L, 5L, NA, 3L, NA), e = c(7L, 8L, 7L, 9L, 
    NA, 12L, 15L)), class = "data.frame", row.names = c("07", 
"06", "05", "04", "03", "02", "01"))




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签