R 累积计数,而不是上一个文件
我试图累计计算所有文件,但出于某种原因,它会计算最后一个文件,并在其余的分析中使用该数字。如何将此代码更改为包含所有文件的计数和唯一计数(共有51个文件)R 累积计数,而不是上一个文件,r,for-loop,count,R,For Loop,Count,我试图累计计算所有文件,但出于某种原因,它会计算最后一个文件,并在其余的分析中使用该数字。如何将此代码更改为包含所有文件的计数和唯一计数(共有51个文件) #将所有文件移到一个列表中 文件列表这是您需要的更正- # Read files out.file <- NULL for (i in 1:length(file_list)){ file <- read.table(file_list[i], header=TRUE, sep=",") out.file <- rb
#将所有文件移到一个列表中
文件列表这是您需要的更正-
# Read files
out.file <- NULL
for (i in 1:length(file_list)){
file <- read.table(file_list[i], header=TRUE, sep=",")
out.file <- rbind(out.file, file)
}
#读取文件
out.file您应该将计数代码移动到循环中,并在循环之前初始化计数变量:
setwd("~/Desktop/GEOG Research/Jordan/compression")
library(plyr)
library(rlang)
library(dplyr)
# Move all files to one list
file_list <- list.files(pattern="Dataset 2.*txt")
# Read files
count_PHONECALLRECORDS <- 0
count_CALLERID <- 0
for (i in 1:length(file_list)){
file <- read.table(file_list[i], header=TRUE, sep=",")
out.file <- rbind(file)
# Count total number phone call records
count_PHONECALLRECORDS <- count_PHONECALLRECORDS + length(out.file$CALLER_ID)
# Count number unique caller id's
count_CALLERID <- count_CALLERID + length(unique(out.file$CALLER_ID))
}
# Construct contingency matrix
tb_1 <- with(out.file, table(CALLEE_PREFIX, CALLER = substr(CALLER_ID, 0, 1)))
colnames(tb_1) <- c("Refugee Caller", "Non-Refugee Caller")
rownames(tb_1) <- c("Refugee Callee", "Non-Refugee Callee", "Unknown Callee")
tb_1
setwd(“~/Desktop/GEOG Research/Jordan/compression”)
图书馆(plyr)
图书馆(rlang)
图书馆(dplyr)
#将所有文件移到一个列表中
文件清单谢谢!我将上面的用户代码添加到这个(初始化out.file)文件中,将一个文件中的一小部分数据添加到您的帖子中。没有它很难帮助您。
setwd("~/Desktop/GEOG Research/Jordan/compression")
library(plyr)
library(rlang)
library(dplyr)
# Move all files to one list
file_list <- list.files(pattern="Dataset 2.*txt")
# Read files
count_PHONECALLRECORDS <- 0
count_CALLERID <- 0
for (i in 1:length(file_list)){
file <- read.table(file_list[i], header=TRUE, sep=",")
out.file <- rbind(file)
# Count total number phone call records
count_PHONECALLRECORDS <- count_PHONECALLRECORDS + length(out.file$CALLER_ID)
# Count number unique caller id's
count_CALLERID <- count_CALLERID + length(unique(out.file$CALLER_ID))
}
# Construct contingency matrix
tb_1 <- with(out.file, table(CALLEE_PREFIX, CALLER = substr(CALLER_ID, 0, 1)))
colnames(tb_1) <- c("Refugee Caller", "Non-Refugee Caller")
rownames(tb_1) <- c("Refugee Callee", "Non-Refugee Callee", "Unknown Callee")
tb_1