Home > Back-end >  How I can sort these two columns using R
How I can sort these two columns using R

Time:09-28

I have a large database, but here is a sample of it:

 df<-read.table (text=" Col1 Col2
        65  NA
        NA  91
        56  NA
        71  71
        67  100
        NA  45
        44  NA
        NA  90
        NA  40
        84  71
        44  63
        NA  20
    ", header=TRUE)

I want to add "1" to Col1 and complete NA in Col1 using Col2. Considering row 2, NA in Col1 would be 91. Here we do not add "1".In Col1, However, we add 1 at the beginning if they do not have NA.

The outcome of interest is:

   Out
    165
    91
    156
    171
    167
    45
    144
    90
    40
    184
    144
    20

CodePudding user response:

Do you mean this?

> with(df, as.numeric(ifelse(is.na(Col1), Col2, sprintf("1%s", Col1))))
 [1] 165  91 156 171 167  45 144  90  40 184 144  20

or

> with(df,ifelse(is.na(Col1), Col2, 100   Col1))
 [1] 165  91 156 171 167  45 144  90  40 184 144  20

CodePudding user response:

With the help of coalesce, we can do

library(dplyr)

df %>%
  transmute(Out = coalesce(suppressWarnings(as.numeric(paste0('1', Col1))),Col2))

#   Out
#1  165
#2   91
#3  156
#4  171
#5  167
#6   45
#7  144
#8   90
#9   40
#10 184
#11 144
#12  20

If the values in Col1 will always be 2 digits we can simplify this to -

df %>% transmute(Out = coalesce(Col1   100, Col2))

CodePudding user response:

Using within.

within(df, {
  na <- is.na(df$Col1)
  Col1 <- 100L   Col1
  Col1[na] <- Col2[na]
  rm(na, Col2)
})
#    Col1
# 1   165
# 2    91
# 3   156
# 4   171
# 5   167
# 6    45
# 7   144
# 8    90
# 9    40
# 10  184
# 11  144
# 12   20

CodePudding user response:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)
df<-read.table (text=" Col1 Col2
        65  NA
        NA  91
        56  NA
        71  71
        67  100
        NA  45
        44  NA
        NA  90
        NA  40
        84  71
        44  63
        NA  20
    ", header=TRUE)
df %>%
  mutate(
    Col1 = Col1 %>% map2(Col2, ~ ifelse(is.na(.x), .y, .x   1))
  )
#>    Col1 Col2
#> 1    66   NA
#> 2    91   91
#> 3    57   NA
#> 4    72   71
#> 5    68  100
#> 6    45   45
#> 7    45   NA
#> 8    90   90
#> 9    40   40
#> 10   85   71
#> 11   45   63
#> 12   20   20

Created on 2021-09-28 by the reprex package (v2.0.1)

CodePudding user response:

library(tidyverse)

df %>%
  mutate(across(c(Col1, Col2), as.numeric),
         Out = if_else(is.na(Col1), Col2, Col1   100))
  • Related