R语言 初学者使用管道

mftmpeh8  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(147)

我是一个初学者,我试图找到最有效的方法来更改我将要创建的许多CSV文件的第一列的名称。一旦我创建了CSV文件,我将它们加载到R中,如下所示:

data <- read.csv('filename.csv')

我已经使用names()函数对单个文件进行了名称更改:

names(data)[1] <- 'Y'

然而,我想找到最有效的方法来组合/管道这个名称更改以读取.csv,以便在打开每个文件时将相同的名称更改应用于每个文件。我尝试编写一个“简单”函数来做到这一点:

addName <- function(data) {
  names(data)[1] <- 'Y'
  data
}

然而,我还没有完全理解写函数的语法,我不能让它工作。

nimxete2

nimxete21#

注解

如果您希望原始的addName函数像这样“改变”现有对象,

x <- data.frame(Column_1 = c(1, 2, 3), Column_2 = c("a", "b", "c"))

# Try (unsuccessfully) to change title of "Column_1" to "Y" in x.
addName(x)

# Print x.
x

请注意,R是通过值而不是通过引用传递的,所以x本身将保持不变

Column_1 Column_2
1        1        a
2        2        b
3        3        c

任何“变化”都可以通过用函数的返回值覆盖x来实现

x <- addName(x)

# Print x.
x

在这种情况下,x本身 * 将 * 明显改变:

Y Column_2
1 1        a
2 2        b
3 3        c

答案

无论如何,这里有一个解决方案,它紧凑地结合了管道(来自magrittr包的%>%)和自定义函数。请注意如果没有换行符和注解,我为了清楚起见添加了它们,这可以压缩为只有几行代码。

# The dplyr package helps with easy renaming, and it includes the magrittr pipe.
library(dplyr)

# ...

filenames <- c("filename1.csv", "filename2.csv", "filename3.csv")

# A function to take a CSV filename and give back a renamed dataset taken from that file.
addName <- function(filename) {
  return(# Read in the named file as a data.frame.
         read.csv(file = filename) %>%
           # Take the resulting data.frame, and rename its first column as "Y";
           # quotes are optional, unless the name contains spaces: "My Column"
           # or `My Column` are needed then.
           dplyr::rename(Y = 1))
}

# Get a list of all the renamed datasets, as taken by addName() from each of the filenames.
all_files <- sapply(filenames, FUN = addName,
                    # Keep the list structure, in which each element is a
                    # data.frame.
                    simplify = FALSE,
                    # Name each list element by its filename, to help keep track.
                    USE.NAMES = TRUE)

事实上,您可以轻松地对任何所需的列执行rename操作,这一切都是一蹴而就的:

dplyr::rename(Y = 1, 'X' = 2, "Z" = 3, "Column 4" = 4, `Column 5` = 5)
sulc1iza

sulc1iza2#

这将读取一个文件名向量,将每个文件的第一列的名称更改为“Y”,并将所有文件存储在列表中。

filenames <- c("filename1.csv","filename2.csv")
addName <- function(filename) {
  data <- read.csv(filename)
  names(data)[1] <- 'Y'
  data
}
files <- list()
for (i in 1:length(filenames)) {
   files[[i]] <- addName(filenames[i])
}

相关问题