English 中文(简体)
有条件使用分行,分行的分行方式不同,分管
原标题:Applying conditional to a subset of rows where rows are different formats in dplyr
  • 时间:2023-12-12 02:14:26
  •  标签:
  • r
  • dplyr

我想在一个数据集中标明一栏下的所有条目均为北美。 令人惊讶的是,一些浏览正在铺设,有些是数字。

用所有数字的栏目来做这项工作,将处以罚款。 让我们说,我们希望有一个指标变量,告诉我们,下一个数据集的a至c栏是否都是北美国家。

df <- data.frame(a = c(1,4,5,NA),
                 b = c(NA,4,2,NA),
                 c = c(3,7,8,NA),
                 d = c(2,1,1,1))

df

# output
   a  b  c d
1  1 NA  3 2
2  4  4  7 1
3  5  2  8 1
4 NA NA NA 1

Using dplyr we can create a variable indicating what we need

df %>%
  rowwise() %>%
    mutate(allNA = case_when(all(is.na(c_across(cols = a:c))) ~ "allNA",
                             TRUE ~ "notAllNA")) %>%
      ungroup()

# output
# A tibble: 4 × 5
      a     b     c     d allNA   
  <dbl> <dbl> <dbl> <dbl> <chr>   
1     1    NA     3     2 notAllNA
2     4     4     7     1 notAllNA
3     5     2     8     1 notAllNA
4    NA    NA    NA     1 allNA  

But if one of the variables is a string instead of a number

df <- data.frame(a = c(1,4,5,NA),
                 b = c(NA,"ava","dillion",NA),
                 c = c(3,7,8,NA),
                 d = c(2,1,1,1))

df

# output
   a       b  c d
1  1    <NA>  3 2
2  4     ava  7 1
3  5 dillion  8 1
4 NA    <NA> NA 1

同一法典有以下错误:

Error in `mutate()`:
ℹ In argument: `allNA = case_when(...)`.
ℹ In row 1.
Caused by error in `case_when()`:
! Failed to evaluate the left-hand side of formula 1.
Caused by error in `vec_c()`:
! Can t combine `a` <double> and `b` <character>.
Run `rlang::last_trace()` to see where the error occurred.

Any help appreciated.

问题回答

实现您预期结果的一种选择是,使用<条码>滚动/代码>和<条码>。

library(dplyr, warn.conflicts = FALSE)

df %>%
  mutate(allNA = if_else(
    rowSums(across(a:c, ~ !is.na(.x))) == 0, "allNA", "notAllNA"
  ))
#>    a       b  c d    allNA
#> 1  1    <NA>  3 2 notAllNA
#> 2  4     ava  7 1 notAllNA
#> 3  5 dillion  8 1 notAllNA
#> 4 NA    <NA> NA 1    allNA




相关问题
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 ...

热门标签