R mutate case_when NOT WORKING (Solved)

なぜか、太字のところしか機能しておらず、結果は6か、NULLになってしまいます。行ごとに、前の結果が消されていっているために最後の6しか残らないのだと思います。

wholedata %>%
mutate(year_ID = case_when(
REF_DATE >= year1start & REF_DATE <= year1end ~1,
REF_DATE >= year2start & REF_DATE <= year2end ~2,
REF_DATE >= year3start & REF_DATE <= year3end ~3,
REF_DATE >= year4start & REF_DATE <= year4end ~4,
REF_DATE >= year5start & REF_DATE <= year5end ~5,
REF_DATE >= year6start & REF_DATE <= year6end ~6,
TRUE ~ -9)-> wholedata2

 

解決しました。最後の行の)が足りませんでした。

wholedata %>%
mutate(year_ID = case_when(
REF_DATE >= year1start & REF_DATE <= year1end ~1,
REF_DATE >= year2start & REF_DATE <= year2end ~2,
REF_DATE >= year3start & REF_DATE <= year3end ~3,
REF_DATE >= year4start & REF_DATE <= year4end ~4,
REF_DATE >= year5start & REF_DATE <= year5end ~5,
REF_DATE >= year6start & REF_DATE <= year6end ~6,
TRUE ~ -9)) -> wholedata2

R question

Someone gave me am example of how to write a simple function. This was great.

addition = function(num1,num2){
answer = num1+num2
return(answer)
}
addition(10,9)
addition(5,4)

However, when I tried to use it, I realized I want to use a text instead of a number in this way.

my_matching = function(dataname){
m.out1<-matchit(data=dataname,control~gpa,method="nearest",ratio=1,
m.order="random", caliper=0.25)
}
my_matching(ABC_data1x)
my_matching(ABC_data2x)

Could you advise how I can run this without an error?

Also, how about a case like this? I used $ and . to indicate which part has to be replaced but this is not R way (This would be in SAS).

my_matching = function(data_N){
m.out1<-
matchit(data=ABC_data&data_n.x,control~gpa,method="nearest",ratio=1,
m.order="random", caliper=0.25)
}
my_matching(1)
my_matching(2)

Thank you!

R package matchit

Example using a R default data matcars.

library(MatchIt)
m.out0<-matchit(data=mtcars,am~hp+drat,exact=c("vs"),method="nearest", ratio=1, m.order="random", caliper=0.25)
m.out0

Using R to do regular data things

# SINGLE AGGREGATE
#sapply(abc[c("GrowthMindset", "SelfEfficacy", "MSelfEfficacy","MathAnxiety","TeacherUse")], mean)

#this just gets means
sapply(abc[c("GrowthMindset", "SelfEfficacy", "MSelfEfficacy","MathAnxiety","TeacherUse")], function(x) mean(x, na.rm=TRUE))

#this gives me a matrix of means
aggregate(cbind(GrowthMindset, SelfEfficacy, MSelfEfficacy) ~ treat, abc, function(x) mean(x, na.rm=TRUE))-> result1

#this gets me a full results
aggregate(cbind(GrowthMindset, SelfEfficacy, MSelfEfficacy, MathAnxiety,TeacherUse) ~ treat, abc,
function(x) c(sum=sum(x), mean=mean(x), min=min(x), q1=quantile(x)[2],
median=median(x), q3=quantile(x)[4], max=max(x), sd=sd(x)))

How to create a dataset that includes aggregate scales and other variables of choice

https://stackoverflow.com/questions/57023935/r-question-how-to-do-a-calculation-of-a-mean-off-multiple-columns-and-select-va/57023959#57023959

 

x2 <- transmute(mydata, circumference,
                average_2items = rowMeans(cbind(age, circumference)),
                age)

 

My example that worked.

> x2<-transmute(time1data,commonID,
+ GrowthMindset=rowMeans(cbind(q0008_0001, q0008_0002, q0008_0003, q0008_0004, q0008_0005, q0008_0006, q0008_0007, q0008_0008)),
+ SelfEfficacy=rowMeans(cbind(q0009_0001, q0009_0002, q0009_0003, q0009_0004, q0009_0005))
+ )

How to get a mean off multiple column values in R

In SAS, this would be:

newvariable=mean(of x1, x2, x3, x4);

Stockoverflow

https://stackoverflow.com/questions/57015735/how-to-get-a-mean-of-multiple-column-values-using-r-dplyr/57015890#57015890

R

Approach 1:

x2 <-subset(time1data,select=c(x1, x2, x3, x4, x5))
time1data$newvar<-rowMeans(x2,na.rm=TRUE)

Approach 2

time1data$newvar<-rowMeans(time1data[,c("q0008_0001", "q0008_0002", "q0008_0003", "q0008_0004", "q0008_0005", "q0008_0006", "q0008_0007", "q0008_0008")])

Approach 3

time1data$newvar<-rowMeans(time1data[,c("q0008_0001", "q0008_0002", "q0008_0003", "q0008_0004", "q0008_0005", "q0008_0006", "q0008_0007", "q0008_0008")],na.rm=TRUE)