-1
$\begingroup$

I have a data set as follows:

salary_old<-c(100,200,300,400,10000,100,10,20,30)
salary_new<-c(200,300,400,500,230,240,30,40,50)
d<-as.Date(c('2019-01-01','2019-01-02','2019-01-03'))
country<-c('USA','UK','IR')
id<-c('A','B','A')
data<-data.frame(id,country,d,salary_new,salary_old)
data<-data %>% arrange(id,country,d)

Then I want to calculate the T.test for the salary old and new for each group using loop or apply function to check if the p-value of each group is less than 0.05.

I wrote codes as follows:

z<-by(data,data$id,apply(data[,4:5],2,function(x,y){
  t.test(x,y)
}))

could you please give me some advice. This the error that I got:

 Error in eval(predvars, data, env) : 
  argument "y" is missing, with no default

I want the output will be like the follows:

id   country  p-value
A     USA      0.9
A     IR       0.9
B     UK       0.34
$\endgroup$
1
  • $\begingroup$ Off-topic here. Please see advice in the Help Center about software-specific questions. $\endgroup$
    – Nick Cox
    Commented Apr 1, 2019 at 11:29

1 Answer 1

1
$\begingroup$

I don't grasp your code very well but I can give you an easier way.

First Make a list of the factors you want to iterate on with split(), then use lapply()

datlist <- split(data , data$id)

lapply(
  X = dat2 ,
  FUN = function(x) {
    t.test(datlist$A[["salary_new"]] , datlist$B[["salary_old"]])
  }
)

results

$`A`

    Welch Two Sample t-test

data:  dat2$A[["salary_new"]] and dat2$B[["salary_old"]]
t = 0.40138, df = 3.5973, p-value = 0.7108
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -270.1249  356.7916
sample estimates:
mean of x mean of y 
 276.6667  233.3333 


$B

    Welch Two Sample t-test

data:  dat2$A[["salary_new"]] and dat2$B[["salary_old"]]
t = 0.40138, df = 3.5973, p-value = 0.7108
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -270.1249  356.7916
sample estimates:
mean of x mean of y 
 276.6667  233.3333 

you can store the output in a list variable and slice to a p-values only

Edit

Enter your full data to get the results

 salary_old<-c(100,200,300,400,10000,100,10,20,30)
salary_new<-c(200,300,400,500,230,240,30,40,50)
d<-as.Date(c('2019-01-01','2019-01-02','2019-01-03'))
country<-c('USA','UK','IR')
id<-c('A','B','A')
data<-data.frame(id,country,d,salary_new,salary_old)
data<-data %>% arrange(id,country,d)

datlist <- split(data ,list(data$id , data$country) )


results<-  lapply(
  1:length(datlist) ,
  FUN = function(x) {
    t.test(datlist[[c(x,4)]] , datlist[[c(x,5)]])
  }
)
$\endgroup$
12
  • $\begingroup$ Thanks I have modified the question and show how I want to see out put, could you please give an advice. $\endgroup$
    – user
    Commented Apr 1, 2019 at 9:25
  • $\begingroup$ I think you are not giving me the full data, right? $\endgroup$
    – Omar113
    Commented Apr 1, 2019 at 10:32
  • $\begingroup$ yes I put only sample data $\endgroup$
    – user
    Commented Apr 1, 2019 at 10:35
  • $\begingroup$ I wrote this line of code but it gives me an error: data1<-data %>% group_by(id,country,d) %>% mutate(test=t.test(salary_old,salary_new)) $\endgroup$
    – user
    Commented Apr 1, 2019 at 10:53
  • $\begingroup$ I have modified the data frame , I want to have t.test apply for each country for all dates in a group $\endgroup$
    – user
    Commented Apr 1, 2019 at 10:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.