[R] substr or split help needed
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Jun 17 21:44:51 CEST 2006
Try this:
# read in lines and replace all occurences of Simula, all occurrences
# of .txt, all commas and all underscores with spaces
Lines <- gsub(".*Simula|.txt|[,_]", " ", readLines("/myfile.dat"))
# reread what is left skipping over column headings and setting it ourself
# If you want factors instead of character data omit as.is=TRUE.
dat <- read.table(textConnection(Lines), as.is = TRUE, skip = 1,
col.names = c("P", "H", "R", "TYPE", "PLAND"))
On 6/17/06, Milton Cezar <miltinho_astronauta at yahoo.com.br> wrote:
> Dear R-friends
>
> I have several data files with about 1,900 lines (records) each. I´m using read.table command to read the files. The files looks like
> LID , TYPE , PLAND
> D:\Bijou-MC\Simula_P005_H100_R001.txt , Forest , NA
> D:\Bijou-MC\Simula_P005_H100_R001.txt , Forest , 10.2
> D:\Bijou-MC\Simula_P010_H100_R001.txt , Forest , 9.2
> ---
>
> My first problem is that some command (like hist(data$PLAND)) say that the data isn´t a numeric one. May be because the first PLAND value are NA? When I done read.table command I used something link:
> data<-read.table (file="xxx.dat", head=T, sep="\,", na.strings="NA").
>
> Another problem is that I need parse the LID column. When I do "print (head(data$LID) I receive the following result (look that the slash was lost on the read):
> D:Bijou-MCSimula_P005_H100_R001.txt
> D:Bijou-MCSimula_P005_H100_R001.txt
> D:Bijou-MCSimula_P010_H100_R001.txt
> Its ok to me, but now I need create the P, H and R columns into the "data" table as a parse of LID column. When I try use the command "p<-substr(data$LID, 19,3)" I got an error message saying that the variable is not char one.
>
> Finally, I´d like drop the LID column and insert the P, H and R into the table.
>
> Thanks for your help!
>
> Kind regards, miltinho
>
>
>
>
>
>
> __________________________________________________
>
>
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list