简体   繁体   English

为 R 中多个数据框的列名添加不同的后缀

[英]Add different suffix to column names on multiple data frames in R

I'm trying to add different suffixes to my data frames so that I can distinguish them after I've merge them.我正在尝试向我的数据框添加不同的后缀,以便在合并它们后可以区分它们。 I have my data frames in a list and created a vector for the suffixes but so far I have not been successful.我将数据框放在一个列表中,并为后缀创建了一个向量,但到目前为止我还没有成功。

data2016 is the list containing my 7 data frames data2016 是包含我的 7 个数据框的列表

new_names <- c("june2016", "july2016", "aug2016", "sep2016", "oct2016", "nov2016", "dec2016")
data2016v2 <- lapply(data2016, paste(colnames(data2016)), new_names)

Your query is not quite clear.您的查询不是很清楚。 Therefore two solutions.因此有两种解决方案。 The beginning is the same for either solution.两种解决方案的开始都是相同的。 Suppose you have these four dataframes:假设您有这四个数据框:

df1x <- data.frame(v1 = rnorm(50),
                   v2 = runif(50))
df2x <- data.frame(v3 = rnorm(60),
                   v4 = runif(60))
df3x <- data.frame(v1 = rnorm(50),
                   v2 = runif(50))
df4x <- data.frame(v3 = rnorm(60),
                   v4 = runif(60))

Suppose further you assemble them in a list, something akin to your data2016 using mget and ls and describing a pattern to match them:假设您进一步将它们组合在一个列表中,类似于您的data2016使用mgetls并描述一个模式来匹配它们:

my_list <-  mget(ls(pattern = "^df\\d+x$"))

The names of the dataframes in this list are the following:此列表中数据框的名称如下:

names(my_list)
[1] "df1x" "df2x" "df3x" "df4x"

Solution 1 :解决方案1

Suppose you want to change the names of the dataframes thus:假设您要更改数据框的名称,如下所示:

new_names <- c("june2016", "july2016","aug2016", "sep2016")

Then you can simply assign new_names to names(my_list) :然后你可以简单地将new_names分配给names(my_list)

names(my_list) <- new_names

And the result is:结果是:

names(my_list)
[1] "june2016" "july2016" "aug2016"  "sep2016"

Solution 2 :解决方案2

You want to add the new_names literally as suffixes to the 'old' names, in which case you would use paste or paste0 thus:您想new_names从字面上添加为“旧”名称的后缀,在这种情况下,您将使用pastepaste0因此:

names(my_list) <- paste0(names(my_list), "_", new_names)

And the result is:结果是:

names(my_list)
[1] "df1x_june2016" "df2x_july2016" "df3x_aug2016"  "df4x_sep2016" 

You could use an index number within lapply to reference both the list and your vector of suffixes.您可以在lapply使用索引号来引用列表和后缀向量。 Because there are a couple steps, I'll wrap the process in a function() .因为有几个步骤,我将把这个过程包装在一个function() (Called an anonymous function because we aren't assigning a name to it.) (称为匿名函数,因为我们没有为其分配名称。)

data2016v2 <- lapply(1:7, function(i) {
  this_data <- data2016[[i]] # Double brackets for a list
  names(this_data) <- paste0(names(this_data), new_names[i]) # Single bracket for vector
  this_data # The renamed data frame to be placed into data2016v2
})

Notice in the paste0() line we are recycling the term in new_names[i] , so for example if new_names[i] is "june2016" and your first data.frame has columns "A" , "B" , and "C" then it would give you this:请注意,在paste0()行中,我们正在回收new_names[i]的术语,因此例如,如果new_names[i]"june2016"并且您的第一个data.frame包含"A""B""C"那么它会给你这个:

> paste0(c("A", "B", "C"), "june2016")
[1] "Ajune2016" "Bjune2016" "Cjune2016"

(You may want to add an underscore in there?) (您可能想在其中添加下划线?)

As an aside, it sounds like you might be better served by adding the "june2016" as a column in your data (like say a variable named month with "june2016" as the value in each row) and combining your data using something like bind_rows() from the dplyr package, running it "long" instead of "wide". "june2016"说一句,听起来通过在数据中添加"june2016"作为一列可能会更好地服务(例如说一个名为month的变量,每行中的值为"june2016" )并使用诸如bind_rows()东西组合您的数据bind_rows()dplyr包,运行它“长”而不是“宽”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM