Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中字符串的循环_R_Loops_For Loop - Fatal编程技术网

R中字符串的循环

R中字符串的循环,r,loops,for-loop,R,Loops,For Loop,我正在尝试循环一系列字符: covariate_names <- c("Age_50.", "Age_30.49", "Age_18.29", "Income_High", "Income_Medium", "Income_Low", "DemographicSegment_Value.Seeker", "De

我正在尝试循环一系列字符:

covariate_names <- c("Age_50.", "Age_30.49", "Age_18.29", "Income_High", "Income_Medium", "Income_Low",
                "DemographicSegment_Value.Seeker", "DemographicSegment_Established", "DemographicSegment_Planner", "DemographicSegment_Affluent",
                "DemographicSegment_Digital.Native","Gender_F..", "Gender_M..", "TransactionAmount",
                "TechTxns", "FashionTxns", "TravelTxns", "GamerTxns",
                "Recency", "Frequency", "Monetary", "Breadth", "Consistency", "is_donor", "donation_amt")

协变量名称您需要使用循环将结果存储在列表中。您可以尝试下一个代码:

#Data
covariate_names <- c("Age_50.", "Age_30.49", "Age_18.29", "Income_High", "Income_Medium", "Income_Low",
                     "DemographicSegment_Value.Seeker", "DemographicSegment_Established", "DemographicSegment_Planner", "DemographicSegment_Affluent",
                     "DemographicSegment_Digital.Native","Gender_F..", "Gender_M..", "TransactionAmount",
                     "TechTxns", "FashionTxns", "TravelTxns", "GamerTxns",
                     "Recency", "Frequency", "Monetary", "Breadth", "Consistency", "is_donor", "donation_amt")
#Function
pairs <- function(df, covariate, group){
  pwc <- pairwise_t_test(data = df,
                         formula = as.formula(paste0(covariate,"~",group)),
                         paired = FALSE )
  pwc <- as.data.frame(pwc)
  pwc <- pwc %>% rename(GroupA = group1,
                        GroupB = group2,
                        N_grpA = n1, N_grpB = n2)
  pwc <- pwc[,1:7]
  pwc <- pwc %>%
    mutate_if(is.numeric, round, digits = 6)
  
  #print(pwc)
}
#数据

协变量名称您可以使用以下函数:

library(dplyr)
library(purrr)

pairs <- function(df, covariate, group){
  pwc <- pairwise_t_test(data = df,
                         formula = reformulate(group, covariate),
                         paired = FALSE )
  pwc <- as.data.frame(pwc)
  pwc %>% 
    rename(GroupA = group1,
           GroupB = group2,
           N_grpA = n1, N_grpB = n2) %>%
    select(1:7) %>%
    mutate(across(where(is.numeric), round, digits = 6))
    #For older dplyr
    #mutate_if(is.numeric, round, digits = 6)
  
}
如果要将结果合并到一个数据帧中,请使用
map\u df

result <- map_df(covariate_names, ~pairs(df_test_TS, .x, "w.contextual"))
结果
#List to store results
List <- list()
#Loop
for (i in 1:length(covariate_names)){
  List[[i]] <- pairs(df = df_test_TS, covariate = covariate_names[i], group = "w.contextual")
}
library(dplyr)
library(purrr)

pairs <- function(df, covariate, group){
  pwc <- pairwise_t_test(data = df,
                         formula = reformulate(group, covariate),
                         paired = FALSE )
  pwc <- as.data.frame(pwc)
  pwc %>% 
    rename(GroupA = group1,
           GroupB = group2,
           N_grpA = n1, N_grpB = n2) %>%
    select(1:7) %>%
    mutate(across(where(is.numeric), round, digits = 6))
    #For older dplyr
    #mutate_if(is.numeric, round, digits = 6)
  
}
result <- map(covariate_names, ~pairs(df_test_TS, .x, "w.contextual"))
result <- map_df(covariate_names, ~pairs(df_test_TS, .x, "w.contextual"))