简体   繁体   English

在R中将for循环重写为lapply函数

[英]Re-writing a for-loop as a lapply function in R

I have several files that contain a series of numbers. 我有几个包含一系列数字的文件。 I want to find out what are the common numbers in all the files. 我想找出所有文件中的通用数字。 eg 例如

a.txt
1
2
3
4

b.txt
2
4
9

c.txt
2
3
4
8
10

Output: 2, 4 输出:2,4

The code I wrote using a for loops gives me the correct result. 我使用for循环编写的代码为我提供了正确的结果。

fileList = c("a.txt", "b.txt", "c.txt")

for(i in 1:length(fileList)){

  tempDF = read.table(fileList[1], header = T, stringsAsFactors = F)

  if(i == 1){

    commons = tempDF$x

  }else{
    commons = intersect(commons, tempDF$x)
  }

}

print(commons)

However I have some trouble re-writing it using a lapply function. 但是我在使用lapply函数重写它时遇到了一些麻烦。 How does lapply keep the value of "commons" variables without replacing? 请问如何在不替换的情况下保持“公共”变量的值?

lapply(fileList, function(x) getCommons(x))

getCommons <- function(file){

  fileData = read.table(file, header = T, stringAsFactor = F)

  commons = intersect(commons, fileData)

}

You could make good use of Reduce here. 您可以在此处充分利用Reduce And since in each file you have a single column that is not necessarily a data frame (no column name), we can replace read.table with scan . 而且,由于在每个文件中都有一个不一定是数据帧的列(没有列名),所以我们可以用scan代替read.table This will produce a list of three numeric vectors, making it easier and faster to find the intersection. 这将产生三个数值向量的列表,从而使查找交点变得更加容易和快捷。

Reduce(intersect, lapply(files, scan, quiet = TRUE))
# [1] 2 4

Data creation: 数据创建:

write(1:4, file = "a.txt", sep = "\n")
write(c(1, 2, 4, 9), file = "b.txt", sep = "\n")
write(c(2, 3, 4, 8, 10), file = "c.txt", sep = "\n")
files <- c("a.txt", "b.txt", "c.txt") 

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

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