按条件切换列的值

问题描述 投票:0回答:1

我想使用 R 重新排列 Excel 电子表格中某些单元格的位置。有两列,place_x 和 place_y,坐标不正确。我打算交换这两列中的一些值以确保清洁。当“place_x”的单元格高于 30 时,我想逐行采样“place_x”列的单元格与“place_y”的单元格的值。

下面是我想要更改的 Excel 电子表格的示例: enter image description here

我试过这个...

#######DEFINITION DU WORKING DIRECTORY#####

# Installer la bibliothèque readxl si ce n'est pas déjà fait
install.packages("Rtools")
install.packages("readxl")
install.packages("openxlsx")


# Charger la bibliothèque openxlsx
library(openxlsx)

# Charger la bibliothèque readxl
library(readxl)

# Spécifier le chemin du fichier Excel
data <- read_excel("Example.xlsx")

# Condition to check if place_x is greater than 30
condition <- data$place_x > 30

# Swap values of place_x and place_y where the condition is met
temp <- data[condition, "place_x"]
data[condition, "place_x"] <- data[condition, "place_y"]
data[condition, "place_y"] <- temp

# Write the result to a new Excel file
output_file <- "INVERSE.xlsx"
write.xlsx(data, file = output_file)

我想把这个放在最后:

enter image description here

r conditional-formatting swap
1个回答
0
投票

出于某种原因,我对此进行了一些尝试。以下是可能答案的一些变体:

library(openxlsx2)

# read data as numerics
df <- wb_to_df("/tmp/so.xlsx", types = c(place_x = 1, place_y = 1))
constructive::construct(df)
#> data.frame(
#>   place_x = c(43.2169125, 43.2169125, 6.758107, 43.504441, 43.504441, 6.2203264),
#>   place_y = c(6.2203264, 6.2203264, 43.502058, 6.757837, 6.757837, 43.2169125),
#>   row.names = 2:7
#> )

## going step by step - be careful with place_x/place_y ----
df[c("low", "high")] <- NA

df$low[df$place_x < 30] <- df$place_x[df$place_x < 30]
df$low[df$place_y < 30] <- df$place_y[df$place_y < 30]

df$high[df$place_x >= 30] <- df$place_x[df$place_x >= 30]
df$high[df$place_y >= 30] <- df$place_y[df$place_y >= 30]

df <- df[c("low", "high")]
names(df) <- c("place_x", "place_y")

df
#>    place_x  place_y
#> 2 6.220326 43.21691
#> 3 6.220326 43.21691
#> 4 6.758107 43.50206
#> 5 6.757837 43.50444
#> 6 6.757837 43.50444
#> 7 6.220326 43.21691

## with selections ----
df <- wb_to_df("/tmp/so.xlsx", types = c(place_x = 1, place_y = 1))
df[c("low", "high")] <- NA

sel <- df$place_x < 30
df$low[sel]   <- df$place_x[sel]
df$low[!sel]  <- df$place_y[!sel]

df$high[!sel] <- df$place_x[!sel]
df$high[sel]  <- df$place_y[sel]

df <- df[c("low", "high")]
names(df) <- c("place_x", "place_y")

df
#>    place_x  place_y
#> 2 6.220326 43.21691
#> 3 6.220326 43.21691
#> 4 6.758107 43.50206
#> 5 6.757837 43.50444
#> 6 6.757837 43.50444
#> 7 6.220326 43.21691

## mapply and if ----
df <- wb_to_df("/tmp/so.xlsx", types = c(place_x = 1, place_y = 1))

tdf <- mapply(df$place_x, df$place_y, FUN = function(x, y) if (x < 30) c(x, y) else c(y, x))
df <- as.data.frame(t(tdf))
names(df) <- c("place_x", "place_y")

df
#>    place_x  place_y
#> 1 6.220326 43.21691
#> 2 6.220326 43.21691
#> 3 6.758107 43.50206
#> 4 6.757837 43.50444
#> 5 6.757837 43.50444
#> 6 6.220326 43.21691

## apply and order ----
df <- wb_to_df("/tmp/so.xlsx", types = c(place_x = 1, place_y = 1))
ordr <- apply(df, 1, FUN = function(x) x[order(x)])
df <- as.data.frame(t(ordr))
names(df) <- c("place_x", "place_y")

df
#>    place_x  place_y
#> 2 6.220326 43.21691
#> 3 6.220326 43.21691
#> 4 6.758107 43.50206
#> 5 6.757837 43.50444
#> 6 6.757837 43.50444
#> 7 6.220326 43.21691

## OPs answer ----
df <- wb_to_df("/tmp/so.xlsx", types = c(place_x = 1, place_y = 1))

condition <- df$place_x > 30

temp <- df[condition, "place_x"]
df[condition, "place_x"] <- df[condition, "place_y"]
df[condition, "place_y"] <- temp

df
#>    place_x  place_y
#> 2 6.220326 43.21691
#> 3 6.220326 43.21691
#> 4 6.758107 43.50206
#> 5 6.757837 43.50444
#> 6 6.757837 43.50444
#> 7 6.220326 43.21691

## write data as xlsx ----
write_xlsx(df, file = "my_file.xlsx")
© www.soinside.com 2019 - 2024. All rights reserved.