Need to fix up the file having 6 and 7 columns to be read as 6 columns
only. Here is the working. Can somebody please let me know how do I
maintain the order in which rows were read and append the two files into
one:

> count.fields(textConnection("LL1532Ap,ABC# Depot-A+,,1971,8,2
LL1532Ap,ABC# Depot-A+,Bhutan,1971,6,1
LL1532Ap,ABC# Depot-A+,China,1971,17,1
LL1532Ap,ABC# Depot-A+,China,1971,33,1
LL1532Ap,ABC# Depot-A+,,1971,16,2
LL1532Ap,ABC# Depot-A+,,1971,17,1
LL1532Ap,ABC# Depot-A+,HongKong, Asia,1971,22,1
LL1532Ap,ABC# Depot-A+,HongKong, Asia,1971,49,1
LL1532Ap,ABC# Depot-A+,,1971,20,1
LL1532Ap,ABC# Depot-A+,Kazakhstan,1971,27,1
LL1532Ap,ABC# Depot-A+,,1971,33,1
LL1532Ap,ABC# Depot-A+,Kazakhstan, Asia,1973,15,1
LL1532Ap,ABC# Depot-A+,Romania-Europe,1971,10,1
LL1532Ap,ABC# Depot-A+,Romania-Europe,1973,4,1
LL1532Ap,ABC# Depot-A+,Sanchez-America,1973,9,1
LL1532An,ABC# Depot-A-,,1971,8,2"),sep=",",comment.char = "")
 [1] 6 6 6 6 6 6 7 7 6 6 6 7 6 6 6 6

> filName <- "temp.csv"
> nFields <- count.fields(filName, sep = ',', comment.char = "")
> input <- readLines(filName)
> writeLines(input[nFields == 6], con = (file = "6fields.csv"))
> writeLines(input[nFields == 7], con = (file = "7fields.csv"))

> filName <- "7fields.csv"
> length(count.fields(filName, sep = ',', comment.char = "")) -> nFields2
> input <- readLines(filName)
> for (i in 1:nFields2){
 strsplit(input[i],",")[[1]] -> z
 paste (z[1], z[2], paste('"',z[3],',',z[4],'"',sep =' '),
z[5],z[6],z[7],sep = ',') -> input[i]
}
> result <- read.table(textConnection(input), sep = ',')

Need the output to look like
        Sno ID Title Location Year x y 1 LL1532Ap ABC# Depot-A+  1971 8 2 2
LL1532Ap ABC# Depot-A+ Bhutan 1971 6 1 3 LL1532Ap ABC# Depot-A+ China 1971
17 1 4 LL1532Ap ABC# Depot-A+ China 1971 33 1 5 LL1532Ap ABC# Depot-A+  1971
16 2 6 LL1532Ap ABC# Depot-A+  1971 17 1 7 LL1532Ap ABC# Depot-A+ HongKong,
Asia 1971 22 1 8 LL1532Ap ABC# Depot-A+ HongKong, Asia 1971 49 1 9
LL1532Ap ABC#
Depot-A+  1971 20 1 10 LL1532Ap ABC# Depot-A+ Kazakhstan 1971 27 1 11
LL1532Ap ABC# Depot-A+  1971 33 1 12 LL1532Ap ABC# Depot-A+ Kazakhstan, Asia
1973 15 1 13 LL1532Ap ABC# Depot-A+ Romania-Europe 1971 10 1 14 LL1532Ap ABC#
Depot-A+ Romania-Europe 1973 4 1 15 LL1532Ap ABC# Depot-A+ Sanchez-America
1973 9 1 16 LL1532An ABC# Depot-A-  1971 8 2

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to