Need help with R

Thanks for helping me with R's ifelse.

This creates a fake dataset.
test = expand.grid(a = c(1, 2, 3, 4), b = c(1, 2, 3))

What I want to do is to something like this:

if a=3 then do;

if b=1 then news="3 and 1";

if b=2 then news="3 and 2";

end;

 

This below does half of the job.  Could you advise?

test$news <- ifelse(test$a == 3,ifelse(test$b == 1, "3 and 1",""),"")

Please email me at k u e k a w a @gmail.com .

Thanks.

R function example

library(broom)
library(compute.es)
library(dplyr)
library(forcats)
library(FSA)
library(gapminder)
library(ggplot2)
library(gmodels)
library(haven)
library(here)
library(leaflet)
library(magrittr)
library(markdown)
library(MatchIt)
library(plyr)
library(psych)
library(purrr)
library(readr)
library(readxl)
library(sas7bdat)
library(sf)
library(sqldf)
library(stringr)
library(summarytools)
library(tidyverse)
library(tmap)
library(tmaptools)
library(descr)
library(writexl)

library(tidyverse)

table1

show_example<-function(var1,var2){

var1_d <-deparse(substitute(var1))
var2_d <-deparse(substitute(var2))

table1 %>%
mutate(prop_cases=table1[[var1_d]]/table1[[var2_d]]) -> temp123
Summarize(temp123$prop_cases ~ country, data= temp123) -> kaz

}

result1<-show_example(var1=cases,var2=population)
result2<-show_example(var1=cases,var2=cases)

R Function example -- from Datacamp.com

https://campus.datacamp.com/courses/introduction-to-writing-functions-in-r/all-about-arguments?ex=2

 

# Set the default for n to 5
cut_by_quantile <- function(x, n, na.rm, labels, interval_type) {
probs <- seq(0, 1, length.out = n + 1)
qtiles <- quantile(x, probs, na.rm = na.rm, names = FALSE)
right <- switch(interval_type, "(lo, hi]" = TRUE, "[lo, hi)" = FALSE)
cut(x, qtiles, labels = labels, right = right, include.lowest = TRUE)
}

# Remove the n argument from the call
cut_by_quantile(
n_visits,
n = 5,
na.rm = FALSE,
labels = c("very low", "low", "medium", "high", "very high"),
interval_type = "(lo, hi]"
)

R question

I am trying to create this function, but I think the problem parts are where I tried to put macro tokens (e.g., var1, var2, var3) within "".    I'm getting error messages.  Any suggestions welcome.  Rでファンクションを書いているのですが、” ”の間に、var1,var2,var3を入れるとエラーが出ます。どうしたらいいでしょうか? This is the error message:

Error in eval(cols[[col]], .data, parent.frame()) : 

object 'var3.y' not found

 

<ここから>

 

make_tables<-function(var1,var2,var3){
analysis_data %>%
mutate(difference=var3.y-var3.x) -> analysis_data_b
analysis_data_c<-filter(analysis_data_b,difference >= 0)

result01pre <-Summarize(var3.x ~ group.x, data= analysis_data_c)
result01pre$test_type<-"Pretest"
result01pre$surveyID<- "var2"
result01pre$tableID<- "var1"
result01pre$item<- "var3.x"
result01pre=subset(result01pre,select=c(tableID, surveyID, item,test_type,group.x,n,mean))

result01post<-Summarize(var3.y ~ group.y, data= analysis_data_c)
result01post$item<- "var3.y"
result01post$test_type<-"Posttest"
result01post=subset(result01post,select=c(item,test_type,group.y,n,mean))

result01diff<-Summarize(difference ~ group.x, data= analysis_data_c)
result01diff$item<- "difference"
result01diff$test_type<-"Difference"
result01diff=subset(result01diff,select=c(item,test_type,group.x,n,mean,sd))

all01<-merge(result01pre,result01post,by.x="group.x",by.y="group.y",all.x = TRUE, all.y = TRUE)
all01<-merge(all01,result01diff,by.x="group.x",by.y="group.x",all.x = TRUE, all.y = TRUE)
#Paired t-test algorithm

all01 %>%
mutate(t_score = (t_score=mean/(sd/sqrt(n)))) %>%
mutate(sig_test=case_when(
t_score < 1.96 ~"ns",
t_score >= 1.96 ~"sig")) ->all01
}}

kaz1 <- make_tables(30,1,miss_5_d_affects_n)