Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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_Dplyr - Fatal编程技术网

R 如何从一组列整齐地创建多个列?

R 如何从一组列整齐地创建多个列?,r,dplyr,R,Dplyr,我希望使用mutate的非交叉函数来创建多个列。我的问题是函数中的变量会随着交叉变量的变化而变化。下面是一个例子: needs=c('Sepal.Length','Petal.Length') iris %>% mutate_at(needs, ~./'{col}.Width') 这显然不起作用,但我想用萼片。长度除以萼片。宽度,花瓣。长度除以花瓣。宽度。这是一个基本的R方法,基于你想要分割的列有相似的名称模式 res <- sapply(split.default(iris[-n

我希望使用mutate的非交叉函数来创建多个列。我的问题是函数中的变量会随着交叉变量的变化而变化。下面是一个例子:

needs=c('Sepal.Length','Petal.Length')
iris %>% mutate_at(needs, ~./'{col}.Width')

这显然不起作用,但我想用萼片。长度除以萼片。宽度,花瓣。长度除以花瓣。宽度。

这是一个基本的R方法,基于你想要分割的列有相似的名称模式

res <- sapply(split.default(iris[-ncol(iris)], sub('\\..*', '', names(iris[-ncol(iris)]))), function(i) i[1] / i[2])
iris[names(res)] <- res
head(iris)

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Petal.Petal.Length Sepal.Sepal.Length
#1          5.1         3.5          1.4         0.2  setosa               7.00           1.457143
#2          4.9         3.0          1.4         0.2  setosa               7.00           1.633333
#3          4.7         3.2          1.3         0.2  setosa               6.50           1.468750
#4          4.6         3.1          1.5         0.2  setosa               7.50           1.483871
#5          5.0         3.6          1.4         0.2  setosa               7.00           1.388889
#6          5.4         3.9          1.7         0.4  setosa               4.25           1.384615

res这里有一个基本的R方法,它基于要划分的列具有相似的名称模式

res <- sapply(split.default(iris[-ncol(iris)], sub('\\..*', '', names(iris[-ncol(iris)]))), function(i) i[1] / i[2])
iris[names(res)] <- res
head(iris)

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Petal.Petal.Length Sepal.Sepal.Length
#1          5.1         3.5          1.4         0.2  setosa               7.00           1.457143
#2          4.9         3.0          1.4         0.2  setosa               7.00           1.633333
#3          4.7         3.2          1.3         0.2  setosa               6.50           1.468750
#4          4.6         3.1          1.5         0.2  setosa               7.50           1.483871
#5          5.0         3.6          1.4         0.2  setosa               7.00           1.388889
#6          5.4         3.9          1.7         0.4  setosa               4.25           1.384615

res我认为你的
需求
应该是两个专栏中的共同点

您可以根据
needs
中的模式选择列,并根据位置划分数据<代码>
:=
用于指定新列的名称

library(dplyr)
library(rlang)

needs = c('Sepal','Petal')

purrr::map_dfc(needs, ~iris %>% 
                        select(matches(.x)) %>%
                        transmute(!!paste0(.x, '_divide') := .[[1]]/.[[2]]))

#    Sepal_divide Petal_divide
#1    1.457142857  7.000000000
#2    1.633333333  7.000000000
#3    1.468750000  6.500000000
#4    1.483870968  7.500000000
#...
#...

如果您想将这些列添加为新列,您可以使用
iris
执行上面的
bind_cols
操作,我认为您的
需求
应该是这两个列中常见的

您可以根据
needs
中的模式选择列,并根据位置划分数据<代码>
:=
用于指定新列的名称

library(dplyr)
library(rlang)

needs = c('Sepal','Petal')

purrr::map_dfc(needs, ~iris %>% 
                        select(matches(.x)) %>%
                        transmute(!!paste0(.x, '_divide') := .[[1]]/.[[2]]))

#    Sepal_divide Petal_divide
#1    1.457142857  7.000000000
#2    1.633333333  7.000000000
#3    1.468750000  6.500000000
#4    1.483870968  7.500000000
#...
#...
如果要将这些列添加为新列,可以使用
iris
执行上述操作