Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中将字符串转换为表_R - Fatal编程技术网

在R中将字符串转换为表

在R中将字符串转换为表,r,R,我对R完全不熟悉,我正在制作一个程序,用一个字符串来解方程组,例如: my_string <- "3x+2y=0; -1x+4y=-1; 13x-2y=11" 提前谢谢。您应该使用strsplit根据逗号进行拆分,然后将其转换为数值 vals <- as.numeric(strsplit(my_string, ",")[[1]]) x <- matrix(vals, ncol=3,nrow=3,byrow=TRUE) x # [,1] [,2] [,3] # [1

我对R完全不熟悉,我正在制作一个程序,用一个字符串来解方程组,例如:

my_string <- "3x+2y=0; -1x+4y=-1; 13x-2y=11" 

提前谢谢。

您应该使用
strsplit
根据逗号进行拆分,然后将其转换为数值

vals <- as.numeric(strsplit(my_string, ",")[[1]])
x <- matrix(vals, ncol=3,nrow=3,byrow=TRUE)
x
#      [,1] [,2] [,3]
# [1,]    3    2    0
# [2,]   -1    4   -1
# [3,]   13   -2   11

vals您可以使用
scan
解析文件或字符向量中的分隔值

matrix(scan(text = my_string, sep = ",", what = 1L), ncol = 3, byrow = TRUE)
#Read 9 items
#     [,1] [,2] [,3]
#[1,]    3    2    0
#[2,]   -1    4   -1
#[3,]   13   -2   11
vals <- as.numeric(strsplit(my_string, ",")[[1]])
x <- matrix(vals, ncol=3,nrow=3,byrow=TRUE)
x
#      [,1] [,2] [,3]
# [1,]    3    2    0
# [2,]   -1    4   -1
# [3,]   13   -2   11
matrix(scan(text = my_string, sep = ",", what = 1L), ncol = 3, byrow = TRUE)
#Read 9 items
#     [,1] [,2] [,3]
#[1,]    3    2    0
#[2,]   -1    4   -1
#[3,]   13   -2   11