Create Matrix by using R program get a input from the user or run time


#1

I am the beginner of R language.I want to create a matrix by using get a input from the user
for example

readinteger <- function(x,y)
{ 
  x <- readline(prompt="Enter an integer x: ")
  y <- readline(prompt="Enter an integer y: ")
  
  return(as.integer(c(x,y)))
}
print (readinteger ())
m <- matrix (1:100 ,nrow = x, ncol = y)
print(m)

Can you please give me some valuable suggestions


#2

Here's an example to get you started, keep in mind that the data in the matrix is somewhat defined by its dimensions which is why I've told it to create a matrix of all NA.

matrix_maker <- function() {
    
    x <- as.integer(readline("Number of rows: "))
    y <- as.integer(readline("Number of cols: "))
    
    matrix(data = NA,
           nrow = x,
           ncol = y)
}

matrix_maker()

This will prompt the user to input the number of rows and columns in the console of the new matrix.


#3

A few references that might help explain some of the differences between the code you started with and @ttrodrigz's example:

Using (or not using) print():

Required arguments in functions and using (or not using) return()

How R assigns values to variables


#4

very thanks for your reply me answer....


#5

very thanks for your reply me question and answer....