Home > Mobile >  Change the name position between two differents columns values in R
Change the name position between two differents columns values in R

Time:12-23

I have a data.frame that shows the correlation between water quality and land use parameters. In this data.frame you have the columns xName, yName, corr, and p.value. However, in the column xName, yName was not a separate column just for quality and solo use.

For example, I would like only the water quality data in the xName column, and in the yName column, only the land use data, since the order would not change the value of corr and p.value

Initially, I thought of creating the new columns with mutate where it shows whether xName and yName is land use and water quality, then using case_when and recode to switch the possibility names.

But I can't move forward, because it returns an error and doesn't change the name.

df=read.table(text="xName   yName   corr    p.value classe_tipox2   classe_tipoy2
IQA_mean    soil    0.639727697 0.025073852 water   land
OD_mean veg 0.60989011  0.03029001  water   land
OD_mean grass   -0.576923077    0.042537186 water   land
soil    N_Total_mean    0.604577823 0.037305053 land    water
crop    N_Total_mean    0.695600561 0.012007646 land    water
crop    P_Total_mean    -0.624544589    0.029931227 land    water
DBO_mean    veg 0.797202797 0.003161252 water   land
DBO_mean    city    0.756757445 0.004382975 water   land
DBO_mean    grass   -0.825174825    0.001718596 water   land
veg P_Total_mean    -0.587412587    0.048844858 land    water
P_Total_mean    grass   0.629370629 0.03239475  water   land", sep="", header=TRUE)%>%
   
  #change names positions
    mutate(xName=case_when(
    classe_tipox2=='land' & classe_tipoy2=='water' ~ recode(xName=yName )
  ))

I would like xName to be renamed to yName when xName is of type land use.

CodePudding user response:

Another option (though a little verbose). First, I put the data into long format, then use ifelse statements to "fix" the xName and yName, then I pivot the data back to wide format.

library(tidyverse)

df %>%
  pivot_longer(c(xName, yName)) %>%
  mutate(
    name = ifelse(
      name == "xName" &
        classe_tipox2 == 'land' &
        classe_tipoy2 == 'water',
      "yName",
      ifelse(
        name == "yName" &
          classe_tipox2 == 'land' &
          classe_tipoy2 == 'water',
        "xName",
        name
      )
    )
  ) %>%
  pivot_wider(names_from = name, values_from = value) %>% 
  select(5:6, 1:4) %>% 
  mutate(classe_tipox2 = "water", classe_tipoy2 = "land")

Output

# A tibble: 11 × 6
   xName        yName   corr p.value classe_tipox2 classe_tipoy2
   <chr>        <chr>  <dbl>   <dbl> <chr>         <chr>        
 1 IQA_mean     soil   0.640 0.0251  water         land         
 2 OD_mean      veg    0.610 0.0303  water         land         
 3 OD_mean      grass -0.577 0.0425  water         land         
 4 N_Total_mean soil   0.605 0.0373  water         land         
 5 N_Total_mean crop   0.696 0.0120  water         land         
 6 P_Total_mean crop  -0.625 0.0299  water         land         
 7 DBO_mean     veg    0.797 0.00316 water         land         
 8 DBO_mean     city   0.757 0.00438 water         land         
 9 DBO_mean     grass -0.825 0.00172 water         land         
10 P_Total_mean veg   -0.587 0.0488  water         land         
11 P_Total_mean grass  0.629 0.0324  water         land  

CodePudding user response:

Please find below one simple solution using dplyr and two ifelse statements. Please note that I simplify your conditions in ifelse because it seems enough.

Reprex

  • Code
library(dplyr)

  df %>% 
    mutate(xName2 = ifelse(classe_tipox2 == 'land', yName, xName),
           yName = ifelse(classe_tipoy2 == 'water', xName, yName)) %>% 
    select(-xName) %>% 
    relocate(xName2, .before = yName) %>% 
    rename_with(.cols = 1, ~"xName")
  • Output
#>           xName yName       corr     p.value classe_tipox2 classe_tipoy2
#> 1      IQA_mean  soil  0.6397277 0.025073852         water          land
#> 2       OD_mean   veg  0.6098901 0.030290010         water          land
#> 3       OD_mean grass -0.5769231 0.042537186         water          land
#> 4  N_Total_mean  soil  0.6045778 0.037305053          land         water
#> 5  N_Total_mean  crop  0.6956006 0.012007646          land         water
#> 6  P_Total_mean  crop -0.6245446 0.029931227          land         water
#> 7      DBO_mean   veg  0.7972028 0.003161252         water          land
#> 8      DBO_mean  city  0.7567574 0.004382975         water          land
#> 9      DBO_mean grass -0.8251748 0.001718596         water          land
#> 10 P_Total_mean   veg -0.5874126 0.048844858          land         water
#> 11 P_Total_mean grass  0.6293706 0.032394750         water          land

Created on 2021-12-22 by the reprex package (v2.0.1)

CodePudding user response:

A base R approach

# get the conditional logic
chose <- df$classe_tipox2 == "land"

# then select the columns and bind with the rest of the data
cbind(setNames(data.frame(t(sapply(seq_along(chose), function(x) 
  'if'(chose[x], c(df$yName[x],df$xName[x]), c(df$xName[x],df$yName[x]))))), 
  colnames(df[,1:2])), df[,-c(1,2)])

          xName yName       corr     p.value classe_tipox2 classe_tipoy2
1      IQA_mean  soil  0.6397277 0.025073852         water          land
2       OD_mean   veg  0.6098901 0.030290010         water          land
3       OD_mean grass -0.5769231 0.042537186         water          land
4  N_Total_mean  soil  0.6045778 0.037305053          land         water
5  N_Total_mean  crop  0.6956006 0.012007646          land         water
6  P_Total_mean  crop -0.6245446 0.029931227          land         water
7      DBO_mean   veg  0.7972028 0.003161252         water          land
8      DBO_mean  city  0.7567574 0.004382975         water          land
9      DBO_mean grass -0.8251748 0.001718596         water          land
10 P_Total_mean   veg -0.5874126 0.048844858          land         water
11 P_Total_mean grass  0.6293706 0.032394750         water          land
  • Related