使用 R 在列之间交换值

swap values between columns using R

我有一个名为 cloud 的数据集,如下所示:

"Rainfall, Treatment
274.7, Seeded
274.7, Seeded
Seeded, 255
242.5, Seeded
200.7, Seeded
198.6, Seeded
129.6, Seeded
119, Seeded
118.3, Seeded
115.3, Seeded
92.4, Seeded
40.6, Seeded
32.7, Seeded
31.4, Seded
17.5, Seeded"

谁能帮我:

  1. 交换值错位的数据(即应该交换Rainfall == "Seeded"Treatment == 255);和

  2. Treatment == "Seded"中的值的拼写更正为"Seeded"

你需要一个临时数字来交换

temp                <- cloud$Treatment[38]
cloud$Treatment[38] <- cloud$Rainfall[38]
cloud$Rainfall[38]  <- temp
temp                <- NULL

您也可以使用此方法更改拼写:

cloud$Treatment[49] <- "Seeded"

概览

我将错位的值存储在两个单独的向量中。然后在 dplyr::mutate() 中使用了三个 dplyr::if_else() 调用来根据需要清理变量。

# load necessary packages -----
library(tidyverse)

# load necessary data --------
cloud <-
  read_csv("Rainfall, Treatment
274.7, Seeded
           274.7, Seeded
           Seeded, 255
           242.5, Seeded
           200.7, Seeded
           198.6, Seeded
           129.6, Seeded
           119, Seeded
           118.3, Seeded
           115.3, Seeded
           92.4, Seeded
           40.6, Seeded
           32.7, Seeded
           31.4, Seded
           17.5, Seeded")

# store the misplaced text value
misplaced.text <-
  cloud %>% pull(Rainfall) %>% str_subset("^\D.*$")

# store the misplaced numeric value
misplaced.numeric <-
  cloud %>% pull(Treatment) %>% str_subset("^\d.*$")

# update cloud so that misplaced values are swapped -----
# and clean Treatment for mispellings
cloud.clean <-
  cloud %>%
  mutate(Rainfall = if_else(Rainfall %in% misplaced.text &
                              Treatment %in%  misplaced.numeric
                            , misplaced.numeric
                            , Rainfall) %>% as.double()
         , Treatment = if_else(Treatment %in%  misplaced.numeric
                               , misplaced.text
                               , Treatment)
         , Treatment = if_else(Treatment %in% "Seded"
                               , "Seeded"
                               , Treatment))

# view results ----
# note: tibble is only rounding the printed output in console
cloud.clean$Rainfall[1] # [1] 274.7
cloud.clean
# A tibble: 15 x 2
# Rainfall Treatment
#        <dbl> <chr>    
#  1    275.  Seeded   
#  2    275.  Seeded   
#  3    255   Seeded   
#  4    242.  Seeded   
#  5    201.  Seeded   
#  6    199.  Seeded   
#  7    130.  Seeded   
#  8    119   Seeded   
#  9    118.  Seeded   
# 10    115.  Seeded   
# 11     92.4 Seeded   
# 12     40.6 Seeded   
# 13     32.7 Seeded   
# 14     31.4 Seeded   
# 15     17.5 Seeded  

# end of script #

使用更小的例子

df <- data.frame(Rainfall=c('Seeded', '31.4'),
                 Treatment=c('255', 'Seded'),
                 stringsAsFactors = F)
df

  Rainfall Treatment
1   Seeded       255
2     31.4     Seded

可能的解决方案:

# Swap values from col/col2 on row 1 (changing col order)
df[1, c(1,2)] <- df[1, c(2,1)]
# Rename Treatment value on row 2
df[2, c("Treatment")] <- 'Seeded'

df 

  Rainfall Treatment
1      255    Seeded
2     31.4    Seeded