0

我的问题是我不断收到以下错误:

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
no lines available in input

从:

setwd("C:/")

lf = list.files(pattern=".csv") 

treat_file = function(f){
  ff = sub("\\.[[:alnum:]]*$", "", f)
  d = read.csv(f, skip=2, sep=",")
  Var2 = sum(d[,3]*d[,5])
  Var3 = 10000*(1/(sum((d[,1]*d[,2])^2)))
  c(as.numeric(ff), Var2, Var3)
} 

newdata = sapply(lf, treat_file)

.csv 文件如下所示:

Final Score: 570

Final X, Starting X, Score, Velocity, Success
-6,-210,100,3,1
-19,-279,70,4,0
2,-229,90,3,1
0,-210,100,3,1
19,-329,50,4,0
17,-279,70,4,0
etc,
etc,
etc,

最终代码

原来有一个文件是空的,这弄乱了 sapply。在函数中包含消息并使用 lapply 向我展示了事情在哪里发生了冲突,现在一切正常。

setwd("C:/")

# find all the text files
lf = list.files(pattern=".csv") 
#Make sure they're there
View(lf)

# this function works on a single file
treat_file = function(f){
#this will record where r is processing in case there is an error (blank .csv file)
message("currently reading:", f)
#create column with .csv scrubbed
ff = sub("\\.[[:alnum:]]*$", "", f)
#read in .csv files
d = read.csv(f, skip=2, sep=",")
#create a score variable
Var2 = sum(d[,3]*d[,5])
#create a continuous score variable
Var3 = 10000000*(1/(sum(sqrt((d[,1]*d[,3])^2))))
#combine the three variables
c(as.numeric(ff), Var2, Var3)
} 
#This is a second way of checking how importing the .csv files is going
#shows number of rows and how many columns are in that row
lapply(lf, count.fields, sep=",")

#creates data.frame in which the function is applied to all csv files
#transposes data.frame
newdata = t(sapply(lf, treat_file))
#change column names
colnames(newdata)= c("PIN", "score", "continuous")
#Make sure everything looks good
View(newdata)

原帖

这里的研究生试图让我的生活更轻松一些,但 R 编程不是我的专长。非常感谢一些帮助。所以我进行了一个实验,我为每个主题获得了一个 .csv 输出,其标题为 1254.csv,其中四位数字对每个人都是唯一的。我的目标是最终得到一个 data.frame,其中第一个变量是每个人的唯一主题编号,第二个和第三个变量是从每个 .csv 文件计算的数字。我想我应该能够做类似的事情:

object (or environment) = all .csv files #need help figuring out exactly how I get it into a workable object or what-have-you
for(i in 1:ncol (csvfileobject)) { 
Var1$newdata.frame = nameof i 
Var2$newdata.frame = (sum up the numbers in column2 for each csvfile) 
Var3$newdata.frame = (multiply columns 2 and 5 and sum that up for each csvfile) }

显然,我不是在找人为我做所有的“工作”,但我对编程很迷茫,R 的方面,也许可以使用一些方向。谢谢!

4

1 回答 1

2

之后我几乎停止阅读My goal is to end up with an excel sheet,但无论如何,这是一个草图:

# find all the text files
lf = list.files(pattern=".txt") 


# this function works on a single file
treat_file = function(f, ...){

  # magic to strip the filename extension
  ff = sub("\\.[[:alnum:]]*$", "", f)
  # read the data into a data.frame
  d = read.table(f, ...)
  # calculate some stuff with the data
  Var2 = sum(d[ ,2]) # summing all the second column
  Var3 = sum(d[ ,2]*d[ ,5]) # etc.

  # results to be returned
  c(as.numeric(ff), Var2, Var3)

} 

# now we apply the function to all files
sapply(lf, treat_file)
于 2013-05-30T23:05:38.893 回答