Hello, I have a question and need your help urgently. I am new to R but want to learn it. I have several files in a folder which I have imported to R using : temp = list.files(pattern="*.txt")>myfiles = lapply(temp, read.delim)The resulting files are on the workspace stored as List[110]. So they are 110 files in the list. Each file has several different columns and rows. My question: I would like to find and replace -999.99M with NA; Find 'T','C','A','F' and 'Y', delete them from all the 110 files. Then, I want to write.table all the corrected files back to a folder on my computer. Thanks for your help. Atem. ________________________________ From: "r-help-request@r-project.org" <r-help-request@r-project.org> Sent: Friday, April 26, 2013 11:08 AM Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Mailing list subscription confirmation notice for mailing list R-help We have received a request from 129.132.148.130 for subscription of r-help@r-project.org mailing list. To confirm that you want to be added to this mailing list, simply reply to this message, keeping the Subject: header intact. Or visit this web page: https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Or include the following line -- and only the following line -- in a message to r-help-request@r-project.org: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Note that simply sending a `reply' to this message should work from most mail readers, since that usually leaves the Subject: line in the right form (additional "Re:" text in the Subject: is okay). If you do not wish to be subscribed to this list, please simply disregard this message. If you think you are being maliciously subscribed to the list, or have any other questions, send them to r-help-owner@r-project.org. [[alternative HTML version deleted]]
Zilefac Elvis
2013-Apr-27 15:24 UTC
[R] find and replace missing data in several different files
Hello, I have a question and need your help urgently. I am new to R but want to learn it. I have several files in a folder which I have imported to R using : temp = list.files(pattern="*.txt")>myfiles = lapply(temp, read.delim)The resulting files are on the workspace stored as List[110]. So they are 110 files in the list. Each file has several different columns and rows. My question: I would like to find and replace -999.99M with NA; Find 'T','C','A','F' and 'Y', delete them from all the 110 files. Then, I want to write.table all the corrected files back to a folder on my computer. Thanks for your help. Atem. ________________________________ From: "r-help-request@r-project.org" <r-help-request@r-project.org> Sent: Friday, April 26, 2013 11:08 AM Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Mailing list subscription confirmation notice for mailing list R-help We have received a request from 129.132.148.130 for subscription of r-help@r-project.org mailing list. To confirm that you want to be added to this mailing list, simply reply to this message, keeping the Subject: header intact. Or visit this web page: https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Or include the following line -- and only the following line -- in a message to r-help-request@r-project.org: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Note that simply sending a `reply' to this message should work from most mail readers, since that usually leaves the Subject: line in the right form (additional "Re:" text in the Subject: is okay). If you do not wish to be subscribed to this list, please simply disregard this message. If you think you are being maliciously subscribed to the list, or have any other questions, send them to r-help-owner@r-project.org. [[alternative HTML version deleted]]
HI,
"Find 'T','C','A','F' and 'Y',
delete them from all the 110 files"
I assume that you meant to replace it with NA.
set.seed(28)
myfiles<- lapply(1:5,function(i)
as.data.frame(matrix(sample(c(LETTERS,"-999.99M"),40*i,replace=TRUE),ncol=(40*i)/8),stringsAsFactors=FALSE))
?myfiles[[1]]
#? V1 V2?????? V3 V4 V5
#1? A? P??????? K? O? Y
#2? C? Z??????? S? M? Y
#3? M? S -999.99M? G? I
#4? X? R??????? M? G? X
#5? C? Q??????? G? A? P
#6? U? W??????? G? B? K
#7? A? N??????? Z? X? R
#8? X? I??????? F? M? K
res<-lapply(myfiles,function(x) {x1<-unlist(x); x1[x1%in%
c("-999.99M","T","C","A","F","Y")]<-NA;as.data.frame(matrix(x1,ncol=ncol(x)),stringsAsFactors=FALSE)})
res[[1]]
?# ? V1 V2?? V3?? V4?? V5
#1 <NA>? P??? K??? O <NA>
#2 <NA>? Z??? S??? M <NA>
#3??? M? S <NA>??? G??? I
#4??? X? R??? M??? G??? X
#5 <NA>? Q??? G <NA>??? P
#6??? U? W??? G??? B??? K
#7 <NA>? N??? Z??? X??? R
#8??? X? I <NA>??? M??? K
lapply(seq_along(res),function(i)
write.table(res[[i]],paste0("file",i,".txt"),row.names=FALSE,quote=FALSE))
A.K.
----- Original Message -----
From: Zilefac Elvis <zilefacelvis at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Saturday, April 27, 2013 11:20 AM
Subject: [R] Help
Hello,
I have a question and need your help urgently. I am new to R but want to learn
it.
I have several files in a folder which I have imported to R using :
temp = list.files(pattern="*.txt")>myfiles = lapply(temp, read.delim)
The resulting files are on the workspace stored as List[110]. So they are 110
files in the list. Each file has several different columns and rows.
My question: I would like to find and replace -999.99M with NA; Find
'T','C','A','F' and 'Y', delete them
from all the 110 files.
Then, I want to write.table all the corrected files back to a folder on my
computer.
Thanks for your help.
Atem.
?
________________________________
From: "r-help-request at r-project.org" <r-help-request at
r-project.org>
Sent: Friday, April 26, 2013 11:08 AM
Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392
Mailing list subscription confirmation notice for mailing list R-help
We have received a request from 129.132.148.130 for subscription of
r-help at r-project.org mailing list.? To confirm that you want to be
added to this mailing list, simply reply to this message, keeping the
Subject: header intact.? Or visit this web page:
? ?
https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d0e2b3adfd392
Or include the following line -- and only the following line -- in a
message to r-help-request at r-project.org:
? ? confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392
Note that simply sending a `reply' to this message should work from
most mail readers, since that usually leaves the Subject: line in the
right form (additional "Re:" text in the Subject: is okay).
If you do not wish to be subscribed to this list, please simply
disregard this message.? If you think you are being maliciously
subscribed to the list, or have any other questions, send them to
r-help-owner at r-project.org.
??? [[alternative HTML version deleted]]
______________________________________________
R-help at 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.
Hi A.K,
Thanks for this great help. I wish to find -999.99M and replace with NA.
All others like T, A, F etc should be deleted from the files.
Thanks for much.
------ Original Message ------
From : arun
To : Zilefac Elvis;
Cc : R help;
Sent : 27-04-2013 16:29
Subject : Re: [R] Help
HI,
"Find 'T','C','A','F' and 'Y',
delete them from all the 110 files"
I assume that you meant to replace it with NA.
set.seed(28)
myfiles<- lapply(1:5,function(i)
as.data.frame(matrix(sample(c(LETTERS,"-999.99
M"),40*i,replace=TRUE),ncol=(40*i)/8),stringsAsFactors=FALSE))
myfiles[[1]]
# V1 V2 V3 V4 V5
#1 A P K O Y
#2 C Z S M Y
#3 M S -999.99M G I
#4 X R M G X
#5 C Q G A P
#6 U W G B K
#7 A N Z X R
#8 X I F M K
res<-lapply(myfiles,function(x) {x1<-unlist(x); x1[x1%in%
c("-999.99M","T","C",
"A","F","Y")]<-NA;as.data.frame(matrix(x1,ncol=ncol(x)),stringsAsFactors=FALSE)
})
res[[1]]
# V1 V2 V3 V4 V5
#1 P K O
#2 Z S M
#3 M S G I
#4 X R M G X
#5 Q G P
#6 U W G B K
#7 N Z X R
#8 X I M K
lapply(seq_along(res),function(i)
write.table(res[[i]],paste0("file",i,".txt"),
row.names=FALSE,quote=FALSE))
A.K.
----- Original Message -----
From: Zilefac Elvis
To: "r-help at r-project.org"
Cc:
Sent: Saturday, April 27, 2013 11:20 AM
Subject: [R] Help
Hello,
I have a question and need your help urgently. I am new to R but want to learn
it.
I have several files in a folder which I have imported to R using :
temp = list.files(pattern="*.txt")>myfiles = lapply(temp, read.delim)
The resulting files are on the workspace stored as List[110]. So they are 110 f
iles in the list. Each file has several different columns and rows.
My question: I would like to find and replace -999.99M with NA; Find
'T','C','A
','F' and 'Y', delete them from all the 110 files.
Then, I want to write.table all the corrected files back to a folder on my comp
uter.
Thanks for your help.
Atem.
________________________________
From: "r-help-request at r-project.org"
Sent: Friday, April 26, 2013 11:08 AM
Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392
Mailing list subscription confirmation notice for mailing list R-help
We have received a request from 129.132.148.130 for subscription of
r-help at r-project.org mailing list. To confirm that you want to be
added to this mailing list, simply reply to this message, keeping the
Subject: header intact. Or visit this web page:
https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d0e2
b3adfd392
Or include the following line -- and only the following line -- in a
message to r-help-request at r-project.org:
confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392
Note that simply sending a `reply' to this message should work from
most mail readers, since that usually leaves the Subject: line in the
right form (additional "Re:" text in the Subject: is okay).
If you do not wish to be subscribed to this list, please simply
disregard this message. If you think you are being maliciously
subscribed to the list, or have any other questions, send them to
r-help-owner at r-project.org.
[[alternative HTML version deleted]]
______________________________________________
R-help at 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.
Hi Atem,
I was able to read the file and removed those suffix letters.? The file had a
lot of formatting issues.? I don't know about the formatting in your other
files.? Also, there were lines? that differ in the number of elements.
If it is similar to the one you send, you can try this:
? con<- file("dt306A009.txt")
?Lines1<- readLines(con)
?close(con)
?Lines2<- Lines1[-1]
?length(Lines2)
#[1] 1140
library(stringr)
Lines3<-str_split(Lines2,"-9999.99M")
Lines4<-
str_trim(unlist(lapply(Lines3,function(x){x[x==""]<-NA;paste(x,collapse="
")})))
Lines5<-gsub("(\\d+)[A-Za-z]","\\1",Lines4)
res<-read.table(text=Lines5,sep="",header=FALSE,fill=TRUE)
?head(res)
#??? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
#1 1917? 1 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2 1917? 2 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#3 1917? 3 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#4 1917? 4 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#5 1917? 5 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#6 1917? 6? 0? 0? 0? 0? 0? 0? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0
#??? V21 V22? V23? V24 V25 V26 V27 V28 V29 V30? V31 V32 V33
#1??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#2??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#3??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#4??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#5??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#6 14.47 2.3 3.34 1.05?? 0?? 0?? 0?? 0?? 0?? 0 1.57?? 0? NA
temp<- list.files(pattern="*.txt")
#Reading multiple files. In this case, I am reading the same file saved with
different names.
?temp
#[1] "dt306A009.txt" "dt306A010.txt"
res<-lapply(temp,function(x) {con<- file(x); Lines1<- readLines(con);
close(con);Lines2<-Lines1[-1]; Lines3<-
str_split(Lines2,"-9999.99M");Lines4<-
str_trim(unlist(lapply(Lines3,function(x){x[x==""]<-NA;paste(x,collapse="
")})));Lines5<-
gsub("(\\d+)[A-Za-z]","\\1",Lines4);res<-
read.table(text=Lines5,sep="",header=FALSE,fill=TRUE) })
?lapply(res,head,2)
#[[1]]
#??? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21
#1 1917? 1 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2 1917? 2 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#? V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33
#1? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#
#[[2]]
#??? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21
#1 1917? 1 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2 1917? 2 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#? V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33
#1? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
A.K.
________________________________
From: Zilefac Elvis <zilefacelvis at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Saturday, April 27, 2013 11:14 PM
Subject: Re: Re: [R] Help
?Hi A.K,
Attached are example files. I have 110 of such and would like to perform the
analysis as described before. I would find T, F, Y, A, C, etc and delete them.
The letters are affixed to values in the data just to communicate an idea. As
before, find all -9999.99M and replace with NA.
Thanks so much.
Atem.
________________________________
From: arun <smartpink111 at yahoo.com>
To: "zilefacelvis at yahoo.com" <zilefacelvis at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, April 27, 2013 7:53 PM
Subject: Re: Re: [R] Help
Hi,
If you wanted to delete "T", "C","A",
"F" and "Y" and replace "-999.99M" with NA, it is
possible, but the result would be stored in a list if
the number of elements deleted from each of the columns are different.?? As you
didn't provide any reproducible example, I don't know whether equal
number of elements are replaced from each columns or not..
set.seed(28)
myfiles<- lapply(1:5,function(i)
as.data.frame(matrix(sample(c(LETTERS,"-999.99M"),40*i,replace=TRUE),ncol=(40*i)/8),stringsAsFactors=FALSE))
res<-lapply(myfiles,function(x){x[x=="-999.99M"]<-NA;lapply(x,function(y)
y[!y%in%c("T","C","A","F","Y")])})
res[[1]]
#$V1
#[1] "M" "X" "U" "X"
#
#$V2
#[1] "P" "Z" "S" "R" "Q"
"W" "N" "I"
#
#$V3
#[1] "K" "S" NA? "M" "G" "G"
"Z"
#
#$V4
#[1] "O" "M" "G" "G" "B"
"X" "M"
#
#$V5
#[1] "I" "X" "P" "K" "R"
"K"
A.K.
________________________________
From: "zilefacelvis at yahoo.com" <zilefacelvis at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, April 27, 2013 9:41 PM
Subject: Re: Re: [R] Help
Hi A.K,
Thanks for ?this great help. I wish to find -999.99M and replace with NA. All
others like T, A, F etc should be deleted from the files.
Thanks for much.
------ Original Message ------
From : arun>To : Zilefac Elvis;
>Cc : R help;
>Sent :
27-04-2013 16:29>Subject : Re: [R] Help
>?
>HI, "Find 'T','C','A','F' and
'Y', delete them from all the 110 files"
I assume that you meant to replace it with NA. set.seed(28)
myfiles<- lapply(1:5,function(i)
as.data.frame(matrix(sample(c(LETTERS,"-999.99M"),40*i,replace=TRUE),ncol=(40*i)/8),stringsAsFactors=FALSE))
myfiles[[1]]
# ?V1 V2 ??????V3 V4 V5
#1 ?A ?P ???????K ?O ?Y
#2 ?C ?Z ???????S ?M ?Y
#3 ?M ?S -999.99M ?G ?I
#4 ?X ?R ???????M ?G ?X
#5 ?C ?Q ???????G ?A ?P
#6 ?U ?W ???????G ?B ?K
#7 ?A ?N ???????Z ?X
?R
#8 ?X ?I ???????F ?M ?K res<-lapply(myfiles,function(x) {x1<-unlist(x);
x1[x1%in%
c("-999.99M","T","C","A","F","Y")]<-NA;as.data.frame(matrix(x1,ncol=ncol(x)),stringsAsFactors=FALSE)})
res[[1]] # ??V1 V2 ??V3 ??V4 ??V5
#1? ??P ???K ???O?
#2? ??Z ???S ???M?
#3 ???M ?S? ????G ???I
#4 ???X ?R ???M ???G ???X
#5? ??Q ???G? ????P
#6 ???U ?W ???G ???B ???K
#7? ??N ???Z ???X ???R
#8 ???X ?I?
????M ???K lapply(seq_along(res),function(i)
write.table(res[[i]],paste0("file",i,".txt"),row.names=FALSE,quote=FALSE))
A.K. ----- Original Message -----
From: Zilefac Elvis?
To: "r-help at r-project.org"?
Cc:
Sent: Saturday, April 27, 2013 11:20 AM
Subject: [R] Help Hello, I have a question and need your help urgently. I am new
to R but want to learn it. I have several files in a folder which I have
imported to R using :
temp = list.files(pattern="*.txt")>myfiles = lapply(temp, read.delim)
The resulting files are on the workspace stored as List[110]. So they are 110
files in the list. Each file has several different columns and rows.
My question: I would like to find and replace -999.99M with NA; Find
'T','C','A','F' and 'Y', delete them
from all the 110 files.
Then, I want to write.table all
the corrected files back to a folder on my computer.
Thanks for your help.
Atem. ________________________________
From: "r-help-request at r-project.org"???Sent: Friday, April 26, 2013
11:08 AM
Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Mailing list
subscription confirmation notice for mailing list R-help We have received a
request from 129.132.148.130 for subscription of r-help at r-project.org mailing
list. ?To confirm that you want to be
added to this mailing list, simply reply to this message, keeping the
Subject: header intact. ?Or visit this web page:
???https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d0e2b3adfd392
Or include the following line -- and only the following line -- in a
message to r-help-request at r-project.org: ???confirm
bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Note that simply sending a `reply'
to this message should work from
most mail readers, since that usually leaves the Subject: line in the
right form (additional "Re:" text in the Subject: is okay). If you do
not wish to be subscribed to this list, please simply
disregard this message. ?If you think you are being maliciously
subscribed to the list, or have any other questions, send them to
r-help-owner at r-project.org. ???[[alternative HTML version deleted]]
______________________________________________
R-help at 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. ?
On Sat, 27 Apr 2013, Zilefac Elvis wrote:> Hi Jeff, > > Attached are sample files from the 110 files. When you delete those letters, > some values will stay put in that same cell. > Atem.It looks like these are fixed format files. You should be using read.fwf and write.fwf instead of read.table and write.table. Read the help for those functions (e.g. ?read.fwf) It is still not clear to me what you want the final result to look like, but you should have better luck reading the data with a fixed format function. You may well find that you no longer need to destroy the contents of the files if you read the data you want correctly. I recommend that you look closely at the source of your data for a detailed description of the data format to aid you in reading the data correctly.> ----- Forwarded Message ----- > From: Zilefac Elvis <zilefacelvis at yahoo.com> > To: arun <smartpink111 at yahoo.com> > Sent: Saturday, April 27, 2013 9:14 PM > Subject: Re: Re: [R] Help > > > ?Hi A.K, > > Attached are example files. I have 110 of such and would like to perform the > analysis as described before. I would find T, F, Y, A, C, etc and delete > them. The letters are affixed to values in the data just to communicate an > idea. As before, find all -9999.99M and replace with NA. > > Thanks so much. > Atem. > > > > ____________________________________________________________________________ > From: arun <smartpink111 at yahoo.com> > To: "zilefacelvis at yahoo.com" <zilefacelvis at yahoo.com> > Cc: R help <r-help at r-project.org> > Sent: Saturday, April 27, 2013 7:53 PM > Subject: Re: Re: [R] Help > > > > > > Hi, > If you wanted to delete "T", "C","A", "F" and "Y" and replace "-999.99M" > with NA, it is possible, but the result would be stored in a list if > the number of elements deleted from each of the columns are different.?? As > you didn't provide any reproducible example, I don't know whether equal > number of elements are replaced from each columns or not.. > > set.seed(28) > myfiles<- lapply(1:5,function(i)as.data.frame(matrix(sample(c(LETTERS,"-999.99M"),40*i,replace=TRUE),ncol=( > 40*i)/8),stringsAsFactors=FALSE)) > res<-lapply(myfiles,function(x){x[x=="-999.99M"]<-NA;lapply(x,function(y) > y[!y%in%c("T","C","A","F","Y")])}) > res[[1]] > #$V1 > #[1] "M" "X" "U" "X" > # > #$V2 > #[1] "P" "Z" "S" "R" "Q" "W" "N" "I" > # > #$V3 > #[1] "K" "S" NA? "M" "G" "G" "Z" > # > #$V4 > #[1] "O" "M" "G" "G" "B" "X" "M" > # > #$V5 > #[1] "I" "X" "P" "K" "R" "K" > > A.K. > ________________________________ > From: "zilefacelvis at yahoo.com" <zilefacelvis at yahoo.com> > To: arun <smartpink111 at yahoo.com> > Cc: R help <r-help at r-project.org> > Sent: Saturday, April 27, 2013 9:41 PM > Subject: Re: Re: [R] Help > > > > > > > > Hi A.K, > > > Thanks for ?this great help. I wish to find -999.99M and replace with NA. > All others like T, A, F etc should be deleted from the files. > > > > > > Thanks for much. > > ------ Original Message ------ > > > > From : arun > >To : Zilefac Elvis; > >Cc : R help; > >Sent : 27-04-2013 16:29 > >Subject : Re: [R] Help > >? > >HI, "Find 'T','C','A','F' and 'Y', delete them from all the 110 files" > I assume that you meant to replace it with NA. set.seed(28) > myfiles<- lapply(1:5,function(i)as.data.frame(matrix(sample(c(LETTERS,"-999.99M"),40*i,replace=TRUE),ncol=( > 40*i)/8),stringsAsFactors=FALSE)) myfiles[[1]] > # ?V1 V2 ??????V3 V4 V5 > #1 ?A ?P ???????K ?O ?Y > #2 ?C ?Z ???????S ?M ?Y > #3 ?M ?S -999.99M ?G ?I > #4 ?X ?R ???????M ?G ?X > #5 ?C ?Q ???????G ?A ?P > #6 ?U ?W ???????G ?B ?K > #7 ?A ?N ???????Z ?X ?R > #8 ?X ?I ???????F ?M ?K res<-lapply(myfiles,function(x) {x1<-unlist(x); > x1[x1%in%c("-999.99M","T","C","A","F","Y")]<-NA;as.data.frame(matrix(x1,ncol=ncol(x) > ),stringsAsFactors=FALSE)}) res[[1]] # ??V1 V2 ??V3 ??V4 ??V5 > #1? ??P ???K ???O? > #2? ??Z ???S ???M? > #3 ???M ?S? ????G ???I > #4 ???X ?R ???M ???G ???X > #5? ??Q ???G? ????P > #6 ???U ?W ???G ???B ???K > #7? ??N ???Z ???X ???R > #8 ???X ?I? ????M ???K lapply(seq_along(res),function(i) > write.table(res[[i]],paste0("file",i,".txt"),row.names=FALSE,quote=FALSE)) > A.K. ----- Original Message ----- > From: Zilefac Elvis? > To: "r-help at r-project.org"? > Cc: > Sent: Saturday, April 27, 2013 11:20 AM > Subject: [R] Help Hello, I have a question and need your help urgently. I am > new to R but want to learn it. I have several files in a folder which I have > imported to R using : > temp = list.files(pattern="*.txt") > >myfiles = lapply(temp, read.delim) > The resulting files are on the workspace stored as List[110]. So they are > 110 files in the list. Each file has several different columns and rows. > My question: I would like to find and replace -999.99M with NA; Find > 'T','C','A','F' and 'Y', delete them from all the 110 files. > Then, I want to write.table all the corrected files back to a folder on my > computer. > Thanks for your help. > Atem. ________________________________ > From: "r-help-request at r-project.org"???Sent: Friday, April 26, 2013 11:08 AM > Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Mailing list > subscription confirmation notice for mailing list R-help We have received a > request from 129.132.148.130 for subscription of r-help at r-project.org > mailing list. ?To confirm that you want to be > added to this mailing list, simply reply to this message, keeping the > Subject: header intact. ?Or visit this web page:???https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d > 0e2b3adfd392 Or include the following line -- and only the following line -- > in a > message to r-help-request at r-project.org: ???confirm > bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Note that simply sending a `reply' > to this message should work from > most mail readers, since that usually leaves the Subject: line in the > right form (additional "Re:" text in the Subject: is okay). If you do not > wish to be subscribed to this list, please simply > disregard this message. ?If you think you are being maliciously > subscribed to the list, or have any other questions, send them to > r-help-owner at r-project.org. ???[[alternative HTML version deleted]] > ______________________________________________ > R-help at 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. ? > > > > > >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k ---------------------------------------------------------------------------
Hi,
You can try this:
Here, I am exporting to the working directory:
temp<- list.files(pattern="*.txt")
?temp
#[1] "dt306A009.txt" "dt306A010.txt"
#here added "_new" to distinguish the new files
?lapply(seq_along(res),function(i)
write.table(res[[i]],paste0(gsub(".txt","",temp[i]),"_new",".txt"),row.names=FALSE,quote=FALSE))
#or write as .csv.
?lapply(seq_along(res),function(i)
write.csv(res[[i]],gsub(".txt",".csv",temp[i]),row.names=FALSE,quote=FALSE)
A.K.
________________________________
From: Zilefac Elvis <zilefacelvis at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Sunday, April 28, 2013 1:07 PM
Subject: Re: Re: [R] Help
Hi A.K,
It works so well with all the 110 files.?
However, how can I export the files such that the original names of the input
files under 'temp' are exported to my output folder?
I did it using the code you provided:
**********************************************************************************************
apply(seq_along(res),function(i)
?
write.table(res[[i]],paste0("file",i,".txt"),row.names=FALSE,quote=FALSE))
******************************************************************************************
However, I wish to paste the strings below instead of sequentially numbered
'paste0("file",i,".txt")'
The strings are my inventory to know which rainfall station belongs to which
location in space.
> temp
? [1] "dt3011120.txt" "dt3011240.txt"
"dt3011887.txt" "dt3012205.txt" "dt3012280.txt"
? [6] "dt3015405.txt" "dt3015523.txt"
"dt3015960.txt" "dt3016119.txt" "dt3022136.txt"
?[11] "dt3023560.txt" "dt3023722.txt"
"dt3024920.txt" "dt3025770.txt" "dt3031093.txt"
?[16] "dt3031400.txt" "dt3031F5F.txt"
"dt3032800.txt" "dt3033880.txt" "dt3034480.txt"
?[21] "dt3034720.txt" "dt3035206.txt"
"dt3036682.txt" "dt3044923.txt" "dt3050519.txt"
?[26] "dt3051R4R.txt" "dt3053250.txt"
"dt3053536.txt" "dt3060L20.txt" "dt3061200.txt"
?[31] "dt3062246.txt" "dt3062427.txt"
"dt3062693.txt" "dt3065999.txt" "dt3067372.txt"
?[36] "dt306A009.txt" "dt3070600.txt"
"dt3072539.txt" "dt3072658.txt" "dt3072723.txt"
?[41] "dt3072920.txt" "dt3073641.txt"
"dt3075040.txt" "dt3076908.txt" "dt3081680.txt"
?[46] "dt4010400.txt" "dt4011441.txt"
"dt4011846.txt" "dt4012120.txt" "dt4012400.txt"
?[51] "dt4013480.txt" "dt4013660.txt"
"dt4014913.txt" "dt4015322.txt" "dt4015360.txt"
?[56] "dt4015960.txt" "dt4016560.txt"
"dt4019040.txt" "dt4019082.txt" "dt4020160.txt"
?[61] "dt4021520.txt" "dt4023240.txt"
"dt4024080.txt" "dt4028060.txt" "dt402DAF0.txt"
?[66] "dt4038400.txt" "dt4038740.txt"
"dt4043900.txt" "dt4045605.txt" "dt4047241.txt"
?[71] "dt4048520.txt" "dt4055079.txt"
"dt4055736.txt" "dt4056120.txt" "dt4056240.txt"
?[76] "dt4057120.txt" "dt4060982.txt"
"dt4061632.txt" "dt4061861.txt" "dt4063560.txt"
?[81] "dt4063755.txt" "dt4064150.txt"
"dt4064600.txt" "dt4068559.txt" "dt4068840.txt"
?[86] "dt406QLD0.txt" "dt4075518.txt"
"dt4083323.txt" "dt4086000.txt" "dt5010240.txt"
?[91] "dt5010485.txt" "dt5010640.txt"
"dt5012080.txt" "dt5012321.txt" "dt5020882.txt"
?[96] "dt5021849.txt" "dt50220M0.txt"
"dt5022759.txt" "dt5022780.txt" "dt5023222.txt"
[101] "dt5030080.txt" "dt5030203.txt"
"dt5030282.txt" "dt5031039.txt" "dt5031111.txt"
[106] "dt5031200.txt" "dt5031320.txt"
"dt5032162.txt" "dt5040680.txt" "dt5041535.txt"
[111] "dt5042004.txt" "dt5042800.txt"
"dt5050920.txt" "dt5052880.txt" "dt5060606.txt"
[116] "dt5061001.txt" "dt5061648.txt"
"dt5062922.txt" "dt506B047.txt"
Thanks so much.
Atem.
________________________________
From: arun <smartpink111 at yahoo.com>
To: Zilefac Elvis <zilefacelvis at yahoo.com>
Sent: Saturday, April 27, 2013 10:57 PM
Subject: Re: Re: [R] Help
HI Atem,
No problem.? Have u tried the code with the 110 files?
________________________________
From: Zilefac Elvis <zilefacelvis at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Sunday, April 28, 2013 12:54 AM
Subject: Re: Re: [R] Help
Oh! I am so happy about this. Thanks so much A.K. You saved me more than 40
hours of work. All other files are similar. Thanks so much.
Atem.
________________________________
From: arun <smartpink111 at yahoo.com>
To: Zilefac Elvis <zilefacelvis at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, April 27, 2013 10:47 PM
Subject: Re: Re: [R] Help
Hi
Atem,
I was able to read the file and removed those suffix letters.? The file had a
lot of formatting issues.? I don't know about the formatting in your other
files.? Also, there were lines? that differ in the number of elements.
If it is similar to the one you send, you can try this:
? con<- file("dt306A009.txt")
?Lines1<- readLines(con)
?close(con)
?Lines2<- Lines1[-1]
?length(Lines2)
#[1]
1140
library(stringr)
Lines3<-str_split(Lines2,"-9999.99M")
Lines4<-
str_trim(unlist(lapply(Lines3,function(x){x[x==""]<-NA;paste(x,collapse="
")})))
Lines5<-gsub("(\\d+)[A-Za-z]","\\1",Lines4)
res<-read.table(text=Lines5,sep="",header=FALSE,fill=TRUE)
?head(res)
#??? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
#1 1917? 1 NA NA NA NA NA NA NA? NA? NA? NA? NA?
NA? NA? NA? NA? NA? NA? NA
#2 1917? 2 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#3 1917? 3 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#4 1917? 4 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#5
1917? 5 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#6 1917? 6? 0? 0? 0? 0? 0? 0? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0?? 0
#??? V21 V22? V23? V24 V25 V26 V27 V28 V29 V30? V31 V32 V33
#1???
NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#2??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#3??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#4??? NA? NA??
NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#5??? NA? NA?? NA?? NA? NA? NA? NA? NA? NA? NA?? NA? NA? NA
#6 14.47 2.3 3.34 1.05?? 0?? 0?? 0?? 0?? 0?? 0 1.57?? 0? NA
temp<- list.files(pattern="*.txt")
#Reading multiple files. In this
case, I am reading the same file saved with different names.
?temp
#[1] "dt306A009.txt" "dt306A010.txt"
res<-lapply(temp,function(x) {con<- file(x); Lines1<- readLines(con);
close(con);Lines2<-Lines1[-1]; Lines3<-
str_split(Lines2,"-9999.99M");Lines4<-
str_trim(unlist(lapply(Lines3,function(x){x[x==""]<-NA;paste(x,collapse="
")})));Lines5<-
gsub("(\\d+)[A-Za-z]","\\1",Lines4);res<-
read.table(text=Lines5,sep="",header=FALSE,fill=TRUE)
})
?lapply(res,head,2)
#[[1]]
#??? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21
#1 1917? 1 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2 1917? 2 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#? V22 V23 V24 V25 V26 V27 V28 V29 V30
V31 V32 V33
#1? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#
#[[2]]
#??? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21
#1 1917? 1 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA?
NA? NA? NA
#2 1917? 2 NA NA NA NA NA NA NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#? V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33
#1? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA
#2? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA? NA?
NA
A.K.
________________________________
From: Zilefac Elvis <zilefacelvis at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Saturday, April 27, 2013 11:14 PM
Subject: Re: Re: [R] Help
?Hi A.K,
Attached are example files. I have 110 of such and would like to
perform the analysis as described before. I would find T, F, Y, A, C, etc and
delete them. The letters are affixed to values in the data just to communicate
an idea. As before, find all -9999.99M and replace with NA.
Thanks so much.
Atem.
________________________________
From: arun <smartpink111 at yahoo.com>
To: "zilefacelvis at yahoo.com" <zilefacelvis at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, April 27, 2013 7:53 PM
Subject: Re: Re: [R] Help
Hi,
If you wanted to delete "T", "C","A",
"F" and "Y" and replace "-999.99M"
with NA, it is possible, but the result would be stored in a list if
the number of elements deleted from each of the columns are different.?? As you
didn't provide any reproducible example, I don't know whether equal
number of elements are replaced from each columns or not..
set.seed(28)
myfiles<- lapply(1:5,function(i)
as.data.frame(matrix(sample(c(LETTERS,"-999.99M"),40*i,replace=TRUE),ncol=(40*i)/8),stringsAsFactors=FALSE))
res<-lapply(myfiles,function(x){x[x=="-999.99M"]<-NA;lapply(x,function(y)
y[!y%in%c("T","C","A","F","Y")])})
res[[1]]
#$V1
#[1] "M" "X" "U" "X"
#
#$V2
#[1] "P" "Z" "S" "R" "Q"
"W" "N" "I"
#
#$V3
#[1] "K" "S" NA? "M" "G" "G"
"Z"
#
#$V4
#[1] "O" "M" "G" "G" "B"
"X" "M"
#
#$V5
#[1] "I" "X" "P" "K" "R"
"K"
A.K.
________________________________
From: "zilefacelvis at yahoo.com" <zilefacelvis at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, April 27, 2013 9:41 PM
Subject: Re: Re: [R] Help
Hi A.K,
Thanks for ?this great help. I wish to find -999.99M and replace with NA. All
others like T, A, F etc should be deleted from the files.
Thanks for much.
------ Original Message ------
From : arun>To : Zilefac Elvis;
>Cc : R help;
>Sent :
27-04-2013 16:29>Subject : Re: [R] Help
>?
>HI, "Find 'T','C','A','F' and
'Y',
delete them from all the 110 files"
I assume that you meant to replace it with NA. set.seed(28)
myfiles<- lapply(1:5,function(i)
as.data.frame(matrix(sample(c(LETTERS,"-999.99M"),40*i,replace=TRUE),ncol=(40*i)/8),stringsAsFactors=FALSE))
myfiles[[1]]
# ?V1 V2 ??????V3 V4 V5
#1 ?A ?P
???????K ?O ?Y
#2 ?C ?Z ???????S ?M ?Y
#3 ?M ?S -999.99M ?G ?I
#4 ?X ?R ???????M ?G ?X
#5 ?C ?Q ???????G ?A ?P
#6 ?U ?W ???????G ?B ?K
#7 ?A ?N ???????Z ?X
?R
#8 ?X ?I ???????F ?M ?K
res<-lapply(myfiles,function(x) {x1<-unlist(x); x1[x1%in%
c("-999.99M","T","C","A","F","Y")]<-NA;as.data.frame(matrix(x1,ncol=ncol(x)),stringsAsFactors=FALSE)})
res[[1]] # ??V1 V2 ??V3 ??V4 ??V5
#1? ??P ???K ???O?
#2? ??Z ???S
???M?
#3 ???M ?S? ????G ???I
#4 ???X ?R ???M ???G ???X
#5? ??Q ???G? ????P
#6 ???U ?W ???G ???B ???K
#7? ??N ???Z ???X ???R
#8 ???X ?I?
????M ???K lapply(seq_along(res),function(i)
write.table(res[[i]],paste0("file",i,".txt"),row.names=FALSE,quote=FALSE))
A.K. ----- Original Message -----
From: Zilefac Elvis?
To: "r-help at r-project.org"?
Cc:
Sent: Saturday, April 27, 2013 11:20 AM
Subject: [R] Help Hello, I have a question and need your help
urgently. I am new to R but want to learn it. I have several files in a folder
which I have imported to R using :
temp = list.files(pattern="*.txt")>myfiles = lapply(temp, read.delim)
The resulting files are on the workspace stored as List[110]. So they are 110
files in the list. Each file has several different columns and rows.
My question: I would like to find and replace -999.99M with NA; Find
'T','C','A','F' and 'Y', delete them
from all the 110 files.
Then, I want to write.table all
the corrected files back to a folder on my computer.
Thanks for your
help.
Atem. ________________________________
From: "r-help-request at r-project.org"???Sent: Friday, April 26, 2013
11:08 AM
Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Mailing list
subscription confirmation notice for mailing list R-help We have
received a request from 129.132.148.130 for subscription of r-help at
r-project.org mailing list. ?To confirm that you want to be
added to this mailing list, simply reply to this message, keeping the
Subject: header intact. ?Or visit this web page:
???https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d0e2b3adfd392
Or include the following line -- and only the following line -- in a
message to r-help-request at r-project.org: ???confirm
bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Note that simply sending a `reply'
to this message should work from
most mail readers, since that usually leaves the Subject:
line in the
right form (additional "Re:" text in the Subject: is okay). If you do
not wish to be subscribed to this list, please simply
disregard this message. ?If you think you are being maliciously
subscribed to the list, or have any other questions, send them to
r-help-owner at r-project.org. ???[[alternative HTML version deleted]]
______________________________________________
R-help at 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. ??
Hi dear all I want to analysis of covariance with R software. What should I do? I would like to have the answer as soon as possible because I have a presentation in the following 8 hours. Thanks a universe. _________________ Best Regards [[alternative HTML version deleted]]
Hi deer all I want to analysis of covariance with R software. What should I do? [[alternative HTML version deleted]]
Hello, I fail to tranfer data from a dataframe to a matrix. var is from a dataframe (and belongs still to the class dataframe) and should look like m (see below).> varvec1 vec3 d1 d2 1 172 173 223 356> dput (var)structure(list(vec1 = 172L, vec3 = 173L, d1 = 223L, d2 = 356L), .Names c("vec1", "vec3", "d1", "d2"), row.names = 1L, class = "data.frame")> m #THIS IS THE AIM[,1] [,2] [1,] 172 223 [2,] 173 356> dput (m)structure(c(172, 173, 223, 356), .Dim = c(2L, 2L)) How can I transform var to m? Thanks akhgar
hi deer all Estimate KM survival probabilities for each categories of RX means ?treatment? and ?placebo? separately> surv.Rx<-survfit(Surv(SURVT,STATUS)~strata(RX),data=rem.data)when write that command. it doesnt run. what should i do? thanks
hi all this command used tt function for all variables. How can i define a different function for each variable? > exCox.all<-coxph(Surv(SURVT,STATUS) ~ RX+LOGWBC+SEX+tt(RX)+tt(LOGWBC)+tt(SEX), data=rem.data,tt=function(x,t,...) log(t)*x)) thank you