本文介绍了在管道R工作流程中为大多数data.frame变量名添加前缀或后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为数据框架中的大多数变量名添加一个后缀或前缀,通常在它们以某种方式进行转换之前和执行连接之前。我没有办法做到这一点,而不破坏我的管道。



例如,使用这些数据:



$($ d $)
set.seed(1)
dat14< - data.frame(ID = 1:10,speed = runif(10) ,power = rpois(10,1),
force = rexp(10),class = rep(c(a,b),5))

我想得到这个结果(注意变量名):

 code> class speed_mean_2014 power_mean_2014 force_mean_2014 
1 a 0.5572500 0.8 0.5519802
2 b 0.2850798 0.6 1.0888116

我目前的做法是:

  means14<  -  dat14%>%
group_by class)%>%
select(-ID)%>%
summarise_each(funs(mean(。)))

名称(means14)[2:名称(means14))]< - paste0(name(means14)[2:length(names(means14))],_mean_2014)

有没有其他选择那个笨拙的最后一条线打破了我的管道?我看过 select() rename(),但不想明确指定每个变量名,因为我通常想重命名除以外的所有单个变量,并且可能具有比此示例更广泛的数据框架。



我是想象一个接近这个补全函数的最终管道命令:

  appendname(cols = 2:n,str =_mean_2014 ,placement =suffix)

据我所知,哪个不存在。 >

解决方案

发布此问题后经过其他实验,我发现 setNames 功能将在管道返回数据框架时起作用。

  dat14%>%
group_by )%>%
select(-ID)%>%
summarise_each(funs(mean(。)))%>%
setNames(c(names ],paste0(name(。)[ - 1],_ mean_2014)))

class speed_mean_2014 power_mean_2014 force_me an_2014
1 a 0.5572500 0.8 0.5519802
2 b 0.2850798 0.6 1.0888116


I want to add a suffix or prefix to most variable names in a data.frame, typically after they've all been transformed in some way and before performing a join. I don't have a way to do this without breaking up my piping.

For example, with this data:

library(dplyr)
set.seed(1)
dat14 <- data.frame(ID = 1:10, speed = runif(10), power = rpois(10, 1),
                    force = rexp(10), class = rep(c("a", "b"),5))

I want to get to this result (note variable names):

  class speed_mean_2014 power_mean_2014 force_mean_2014
1     a       0.5572500             0.8       0.5519802
2     b       0.2850798             0.6       1.0888116

My current approach is:

means14 <- dat14 %>%
  group_by(class) %>%
  select(-ID) %>%
  summarise_each(funs(mean(.)))  

names(means14)[2:length(names(means14))] <- paste0(names(means14)[2:length(names(means14))], "_mean_2014")

Is there an alternative to that clunky last line that breaks up my pipes? I've looked at select() and rename() but don't want to explicitly specify each variable name, as I usually want to rename all except a single variable and might have a much wider data.frame than in this example.

I'm imagining a final piped command that approximates this made-up function:

appendname(cols = 2:n, str = "_mean_2014", placement = "suffix")

Which doesn't exist as far as I know.

解决方案

After additional experimenting since posting this question, I've found that the setNames function will work with the piping as it returns a data.frame:

dat14 %>%
  group_by(class) %>%
  select(-ID) %>%
  summarise_each(funs(mean(.))) %>%
  setNames(c(names(.)[1], paste0(names(.)[-1],"_mean_2014"))) 

  class speed_mean_2014 power_mean_2014 force_mean_2014
1     a       0.5572500             0.8       0.5519802
2     b       0.2850798             0.6       1.0888116

这篇关于在管道R工作流程中为大多数data.frame变量名添加前缀或后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 14:35