[R] File data to data.frame

jim holtman jholtman at gmail.com
Tue Mar 13 14:19:15 CET 2012


try this:

> x <- readLines(textConnection("<test.php
+ GET
+ username=user
+ password=pass
+ >
+ <test.php
+ GET
+ username=user1
+ password=pass1
+ >"))
>
> closeAllConnections()
>
> # process & parse the data
> for (i in x){
+     if (grepl("^username", i)) username <- sub(".*=(.*)", '\\1', i)
+     if (grepl("^password", i)){
+         password <- sub(".*=(.*)", "\\1", i)
+         cat("found: user =", username, '  password =', password, '\n')
+     }
+ }
found: user = user   password = pass
found: user = user1   password = pass1
>


On Tue, Mar 13, 2012 at 6:09 AM, b4d <baccic at gmail.com> wrote:
> Hi, I am playing around with some data and I would like to get data that is
> stored in a file like this:
>
> <test.php
> GET
> username=user
> password=pass
>>
> <test.php
> GET
> username=user1
> password=pass1
>>
> ...
>
> to the data.frame structure, how can I do that directly in R, currently I am
> doing parse with bash, but I would like to centralize the procedure and
> learn something new.
>
> Thanks
>
> --
> View this message in context: http://r.789695.n4.nabble.com/File-data-to-data-frame-tp4468671p4468671.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list