在R中跨多个列的else if语句

afdcj2ne  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(93)

我尝试在以下示例数据集中的多个列上应用else if函数:

dput(data_compressed)
structure(list(Day = c(0, 1, 0, 1, 1, 0, 1, 0, 1, 0), act1_001 = c("leisure", 
"leisure", "leisure", "leisure", "leisure", "leisure", "leisure", 
"leisure", "leisure", "leisure"), act1_002 = c("leisure", "leisure", 
"leisure", "leisure", "leisure", "leisure", "leisure", "leisure", 
"leisure", "leisure"), act1_003 = c("leisure", "leisure", "leisure", 
"leisure", "leisure", "leisure", "leisure", "leisure", "leisure", 
"leisure"), act1_004 = c("leisure", "leisure", "leisure", "leisure", 
"leisure", "leisure", "leisure", "leisure", "leisure", "leisure"
), act1_005 = c("leisure", "leisure", "leisure", "leisure", "leisure", 
"leisure", "leisure", "leisure", "leisure", "leisure")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

字符串
我的其他代码的逻辑:如果两个条件(i)Day=1和(ii)以“act1”开头的每一列都用“leisure”编码,则打印“leisure1”。如果Day=0且act1=leisure,则打印“leisure0”。相同的逻辑适用于其余语句。

library(dplyr)
library(tidyverse)

if(data$Day == 1 & data$across(starts_with("act1")) == "leisure") {
  print("leisure1")
} else if(
  data$Day == 0 & data$across(starts_with("act1")) == "leisure") {
  print("leisure0")
} else if(
  data$Day == 1 & data$across(starts_with("act1")) == "work") {
  print("work1")
} else if(
  data$Day == 0 & data$across(starts_with("act1")) == "work") {
  print("work0")
} else if(
  data$Day == 1 & data$across(starts_with("act1")) == "home") {
  print("home1")
} else {
  print("home0")
}


这是错误消息:
警告:未知或未初始化的列:across。错误:尝试应用非函数

lb3vh1jj

lb3vh1jj1#

也许你只是这个意思。

data %>%
  mutate(across(starts_with("act1"), ~ paste0(.x, Day)))
#   Day act1_001 act1_002 act1_003 act1_004 act1_005 act1_006 act1_007
# 1   1       a1       b1       d1       a1       a1       b1       b1
# 2   1       a1       a1       b1       a1       d1       a1       b1
# 3   2       b2       b2       a2       b2       d2       b2       b2
# 4   2       d2       b2       d2       d2       b2       d2       d2
# 5   1       d1       d1       a1       d1       a1       b1       d1

字符串
数据

data <- structure(list(Day = c(1, 1, 2, 2, 1), act1_001 = c("a", "a", "b", "d", "d"), act1_002 = c("b", "a", "b", "b", "d"), act1_003 = c("d", "b", "a", "d", "a"), act1_004 = c("a", "a", "b", "d", "d"), act1_005 = c("a", "d", "d", "b", "a"), act1_006 = c("b", "a", "b", "d", "b"), act1_007 = c("b", "b", "b", "d", "d")), class = "data.frame", row.names = c(NA, -5L))

eimct9ow

eimct9ow2#

从你的逻辑判断,你想在每一列中折叠与Day相关的数字和字母。要在基数R中做到这一点,你可以这样做:

act_cols <- grep("act1", names(data)) # identify columns of interest

data[act_cols] <- lapply(data[act_cols], \(x) paste0(unlist(x), unlist(data[1])))

字符串
输出量:

#   Day act1_001 act1_002 act1_003 act1_004 act1_005 act1_006 act1_007
# 1   1       a1       b1       d1       a1       a1       b1       b1
# 2   1       a1       a1       b1       a1       d1       a1       b1
# 3   2       b2       b2       a2       b2       d2       b2       b2
# 4   2       d2       b2       d2       d2       b2       d2       d2
# 5   1       d1       d1       a1       d1       a1       b1       d1

yr9zkbsy

yr9zkbsy3#

您使用的across不正确。下面是tidyverse中使用case_when()的解决方案:

library(dplyr)
library(tidyr)

data %>%
  pivot_longer(cols = starts_with("act1"), names_to = "activity", values_to = "value") %>% 
  mutate(result = case_when(
    Day == 1 & value == "a" ~ "a1",
    Day == 2 & value == "a" ~ "a2",
    Day == 1 & value == "b" ~ "b1",
    Day == 2 & value == "b" ~ "b2",
    Day == 1 & value == "d" ~ "d1",
    .default =  "d2"
  ))

A tibble: 35 × 4
     Day activity value result
   <dbl> <chr>    <chr> <chr> 
 1     1 act1_001 a     a1    
 2     1 act1_002 b     b1    
 3     1 act1_003 d     d1    
 4     1 act1_004 a     a1    
 5     1 act1_005 a     a1    
 6     1 act1_006 b     b1    
 7     1 act1_007 b     b1    
 8     1 act1_001 a     a1    
 9     1 act1_002 a     a1    
10     1 act1_003 b     b1    
# ℹ 25 more rows
# ℹ Use `print(n = ...)` to see more rows

字符串
数据类型:

Day <- c(1,1,2,2,1)
act1_001 <- c("a","a","b","d","d")
act1_002 <- c("b","a","b","b","d")
act1_003 <- c("d","b","a","d","a")
act1_004 <- c("a","a","b","d","d")
act1_005 <- c("a","d","d","b","a")
act1_006 <- c("b","a","b","d","b")
act1_007 <- c("b","b","b","d","d")

data <- data.frame(Day, act1_001, act1_002, act1_003, act1_004, act1_005, act1_006, act1_007)

相关问题