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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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中的一个问题。希望有人能帮忙: 我有以下数据表(仅显示了一个名为xout的示例): 我试图添加一个新的列,该列的相对频率来自一条法线曲线。我尝试将列factorx拆分为两列,分别称为min和max,以便使用数值传递到dnorm函数中。我所有在r中进行字符串操作的尝试都失败了。我尝试使用: gsub("[^/d]","",strsplit(toString(xout$factorx),","))) 但这失败了。我对r很陌生,所以我相信有更好的方法。你就不能这么做

几个小时以来,我一直在想如何解决R中的一个问题。希望有人能帮忙:

我有以下数据表(仅显示了一个名为xout的示例):

我试图添加一个新的列,该列的相对频率来自一条法线曲线。我尝试将列factorx拆分为两列,分别称为min和max,以便使用数值传递到dnorm函数中。我所有在r中进行字符串操作的尝试都失败了。我尝试使用:

gsub("[^/d]","",strsplit(toString(xout$factorx),",")))
但这失败了。我对r很陌生,所以我相信有更好的方法。

你就不能这么做吗

data.frame(xout, newCol=c(1,2,3,4,...))
当然,你给出的向量可以是任何东西

示例:使用Freq*4添加新列:

data.frame(xout, FreqFour=xout[[2]]*4)
导致

       factorx Freq cumFreq   relative FreqFour
1    (-2,-1.9]   13      13 0.00132626       52
2  (-1.9,-1.8]   18      31 0.00183636       72
3  (-1.8,-1.7]   22      53 0.00224444       88
4  (-1.7,-1.6]   18      71 0.00183636       72
5  (-1.6,-1.5]   22      93 0.00224444       88
6  (-1.5,-1.4]   31     124 0.00316262      124

如果您确实想使用
sub
,那么这里有一种方法。您可以使用
regexp
模式中的
(.)
捕获所需的组,然后将其拾取

min <- as.numeric(sub("\\((.*),.*$", "\\1", xout$factorx))
> min
# [1] -2.0 -1.9 -1.8 -1.7 -1.6 -1.5

max <- as.numeric(sub(".*,(.*)\\]$", "\\1", xout$factorx))
> max
# [1] -1.9 -1.8 -1.7 -1.6 -1.5 -1.4
矩阵行中有
min
max

sub
的另一种变体:您可以首先使用
sub
删除
]
),然后使用
strsplit

sapply(strsplit(sub("\\((.*)\\]", "\\1", xout$factorx), ","), as.numeric)

我想这是一种可能性,但数据框相当大。我在帖子中展示了一个示例。我宁愿避免键入所有可能的值。此外,我不能使用序列,因为与我展示的示例不同,在值的范围内存在跳过。您不必键入值?在示例中,我使用了xout的数据第二列计算新列。你想从你的数据中计算一些东西并添加它,对吗?是的,我想从factorx列中得到数字,所以在上面的示例中,第一行是-2,第二行是-1.9,等等。
# first convert to character (to use `nchar` and `substr`)
xout$factorx <- as.character(xout$factorx)
# first remove the ( and ] and then split by "," and then convert to numeric
sapply(strsplit(substr(xout$factorx, 2, nchar(xout$factorx)-1), ","), as.numeric)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] -2.0 -1.9 -1.8 -1.7 -1.6 -1.5
[2,] -1.9 -1.8 -1.7 -1.6 -1.5 -1.4
sapply(strsplit(sub("\\((.*)\\]", "\\1", xout$factorx), ","), as.numeric)