GGA
GGA

Reputation: 385

User inputs a function argument in r

I'm trying to accept as an argument of a function the user input but i have trouble making it work. My actual code is this:

t.student.2code <- function()
{
  f <- readline(prompt="Select column group A:")
  y <- readline(prompt="Select column group B:")


  t.test(f,y)



  }

Normal t.test

t.test(beaver1$time,beaver1$temp)

    Welch Two Sample t-test

data:  beaver1$time and beaver1$temp
t = 19.399, df = 113, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 1144.924 1405.387
sample estimates:
mean of x  mean of y 
1312.01754   36.86219 

Using beaver1$time and beaver1$temp as arguments in my function it doesn't work

Error in t.test.default(x, y) : not enough 'x' observations
Inoltre: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In var(x) : si è prodotto un NA per coercizione

Upvotes: 0

Views: 123

Answers (2)

doctorG
doctorG

Reputation: 1731

# default data frame (df)

f <- c(1,2,3)
g <- c(3,5,6)
df <- data.frame(f,g)

tfunc1 <- function() {
    print("Available column names:")
    print(names(df))
    col1 <- readline(prompt="Enter column 1 name:\n")[[1]]
    col2 <- readline(prompt="Enter column 2 name:\n")[[1]]

    t.test(df[[col1]], df[[col2]])
}

tfunc2 <- function(localdf) {
    print("Available column names:")
    print(names(localdf))
    col1 <- readline(prompt="Enter column 1 name:\n")[[1]]
    col2 <- readline(prompt="Enter column 2 name:\n")[[1]]

    t.test(localdf[[col1]], localdf[[col2]])
}

tfunc1()
[1] "Available column names:"
[1] "f" "g"
Enter column 1 name:
f
Enter column 2 name:
g

    Welch Two Sample t-test

data:  df[[col1]] and df[[col2]]
t = -2.5298, df = 3.4483, p-value = 0.07459
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -5.7875506  0.4542173
sample estimates:
mean of x mean of y 
 2.000000  4.666667 

tfunc2(df)
[1] "Available column names:"
[1] "f" "g"
Enter column 1 name:
g
Enter column 2 name:
f

    Welch Two Sample t-test

data:  localdf[[col1]] and localdf[[col2]]
t = 2.5298, df = 3.4483, p-value = 0.07459
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.4542173  5.7875506
sample estimates:
mean of x mean of y 
 4.666667  2.000000 

Upvotes: 0

StW
StW

Reputation: 262

Your code looks okay. Note that readline() returns strings, so the function t.test must accept two string parameters.

Simple example:

testinp <- function() {
  c1 <- readline()
  c2 <- readline()
  sum(as.numeric(c1), as.numeric(c2))
}

In case you man t.test from the stats package, passing strings won't work as it expects numerical vectors. Maybe you want to specify variable names like the example below?

c1 <- c(1,2,3)
c2 <- c(2,4,5)
testinp <- function() {
  tmp1 <- readline()
  tmp2 <- readline()
  t.test(get(tmp1), get(tmp2))
}
testinp() # enter "c1" and "c2"

To handle the example your question:

testinp <- function() {
  c1 <- readline()
  c2 <- readline()
  t.test(eval(parse(text=c1)), eval(parse(text=c2)))
}
testinp() # enter "beaver1$time" and "beaver1$temp"

Upvotes: 1

Related Questions