Home > Blockchain >  Writing a program which organizes data entered into a matrix:
Writing a program which organizes data entered into a matrix:

Time:01-18

New to R and programming. The following is the code I have so far.

user_input <- readline(prompt = "Enter the data: ");
input_split <- strsplit(user_input, ",")
rows1<- length(input_split) / 2
finalmatrix <- matrix(c(input_split), nrow = (rows1), ncol = (rows1))
print(finalmatrix)

Could someone tell me why this is not working? Thank you for your help.

This is the output:

enter image description here

CodePudding user response:

The issue is with the list(), when we use the strsplit, the output will be a list, however we need to convert it into a vector and then use

code

user_input <- readline(prompt = "Enter the data: ");
input_split <- unlist(strsplit(user_input, ","))
rows1<- length(input_split) / 2
finalmatrix <- matrix(c(input_split), nrow = (rows1), ncol = (rows1))
print(finalmatrix)
  •  Tags:  
  • r
  • Related