R, How ot use plyr in R , fix Error in filefile, rt cannot open the connection

Опубликовано: 19 Апрель 2021
на канале: gocha's learning space
231
0

demo@ubuntu:~/SelfStudy/R/R-Fl$ ls
data_dir HowToUse_plyr.R log others read_multiple_csv_1st.log read_multiple_csv_1st.R requirements.txt write_csv.R
demo@ubuntu:~/SelfStudy/R/R-Fl$ cat others/*.csv
A,I,U,E,O
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
"","name","age"
"1","A",10
"2","B",20
"3","C",30
"name","age"
"A",10
"B",20
"C",30
demo@ubuntu:~/SelfStudy/R/R-Fl$ cat HowToUse_plyr.R
#! /usr/bin/env Rscript

library(plyr)
#setwd("~/SelfStudy/R/R-Fl")
setwd("~/SelfStudy/R/")
tgt_dir = "others"
files = list.files(path=tgt_dir, pattern="*.csv", full.names=TRUE)
#print(files.num)
#files

a = ldply(files[1], read.csv)
str(a)
print(a)

b = ldply(files[2], read.csv)
str(b)
print(b)


demo@ubuntu:~/SelfStudy/R/R-Fl$ ./HowToUse_plyr.R
Error in file(file, "rt") : cannot open the connection
Calls: ldply ... llply - structure - lapply - FUN - read.table - file
In addition: Warning message:
In file(file, "rt") : cannot open file 'NA': No such file or directory
Execution halted
demo@ubuntu:~/SelfStudy/R/R-Fl$ vi HowToUse_plyr.R
demo@ubuntu:~/SelfStudy/R/R-Fl$ ./HowToUse_plyr.R
'data.frame': 6 obs. of 5 variables:
$ A: int 1 1 1 1 1 1
$ I: int 2 2 2 2 2 2
$ U: int 3 3 3 3 3 3
$ E: int 4 4 4 4 4 4
$ O: int 5 5 5 5 5 5
A I U E O
1 1 2 3 4 5
2 1 2 3 4 5
3 1 2 3 4 5
4 1 2 3 4 5
5 1 2 3 4 5
6 1 2 3 4 5
'data.frame': 3 obs. of 3 variables:
$ X : int 1 2 3
$ name: Factor w/ 3 levels "A","B","C": 1 2 3
$ age : int 10 20 30
X name age
1 1 A 10
2 2 B 20
3 3 C 30