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

When two variables are similar but different by training empty spaces, do this.

levels(as.factor(main$county))

levels(as.factor(arc$county))

I tried to merge two data tables that contain the same variable county.   The simple merge did not work.  It turns out that one variable had trailing empty spaces.

R string macro

https://notstatschat.rbind.io/2018/07/30/quoting-and-macros-in-r/

Also

#id dups
#create fake data
junk <- c(1, "J", 1, "J", 1, "K", 2, "K", 3, "K", 3, "T", 3, "T", 3, "T")
junk_mat <- data.frame(matrix(junk,ncol=2, byrow=T))
#install.packages("data.table") # install it
library(data.table)
junk_tab <- data.table(junk_mat)
colnames(junk_tab) <- c("num_var", "char_var")
junk_tab$num_var <- as.numeric(junk_tab$num_var)
junk_tab

library(psych)

#this one works with char_var in quotes
kaz_macro1<-function(var1,var2){
var1<-describeBy(x=as.numeric(junk_tab$num_var), group=junk_tab[[var2]])
print(var1)
}
kaz_macro1(var1=kc1a,var2="char_var")

#this one works without char_var in quotes
kaz_macro1<-function(var1,var2){
temp_var <-deparse(substitute(var2))
var1<-describeBy(x=as.numeric(junk_tab$num_var), group=junk_tab[[temp_var]])
print(var1)
}
kaz_macro1(var1=kc1a,var2=char_var)

 

How to write a function to do these?

I have a repetitive process in R and I want to get this done in one function.  Could you help?

result1<-Summarize(GrowthMindset ~ treat+ dataID, data=wholedata2)
result1$varname<-"GrowthMindset"
result1$sample_type<-"Whole"

#Scale 2
result2<-Summarize(SelfEfficacy ~ treat+ dataID, data=wholedata2)
result2$varname<-"SelfEfficacy"
result2$sample_type<-"Whole"

#Scale 3
result3<-Summarize(MSelfEfficacy ~ treat+ dataID, data=wholedata2)
result3$varname<-"MSelfEfficacy"
result3$sample_type<-"Whole"

#Scale 4
result4<-Summarize(MathAnxiety ~ treat+ dataID, data=wholedata2)
result4$varname<-"MathAnxiety"
result4$sample_type<-"Whole"

#Scale 5
result5<-Summarize(TeacherUse ~treat+  dataID, data=wholedata2)
result5$varname<-"TeacherUse"
result5$sample_type<-"Whole"

 

The solution (thanks Isaac):

kaz_macro2<-function(var1){
x1<- Summarize(GrowthMindset ~ treat+ dataID, data=var1)
x2<- Summarize(SelfEfficacy ~ treat+ dataID, data=var1)
x3<- Summarize(MSelfEfficacy ~ treat+ dataID, data=var1)
x4<- Summarize(MathAnxiety ~ treat+ dataID, data=var1)
x5<- Summarize(TeacherUse ~ treat+ dataID, data=var1)
as.data.frame(rbind(x1,x2,x3,x4,x5))
}
temp1234<-kaz_macro2(wholedata2)