[R] Import of specific column of many space-delimited text files
Joshua Wiley
jwiley.psych at gmail.com
Sun Jul 18 21:03:13 CEST 2010
Hi,
There might be better ways, but here's something that might help you
get started. Obviously you'll need to tweak a lot of things. If your
files do not have a uniform number of columns and the column you want
is not always in the same position, it may be easier to just read it
all in and then extract the column by name ocne they are in R.
# a little sample data
var1 var2 var3
54 6326 45
66 3452 44
52 5436 43
64 7865 48
54 6534 44
#how you can read in just one column
read.table("clipboard", header = TRUE, sep = " ",
colClasses = c("NULL", "integer", "NULL"))
#Grab a files in your WD that have .txt in them
file.names <- grep(".txt", list.files(), value = TRUE)
#Initialize a list to store all the file you read in
mydata <- vector("list", length = length(file.names))
#Read in data files and assign them to your list
for(i in seq_along(file.names)) {
mydata[[i]] <- read.table(file = file.names[i], sep = " ",
colClasses = c("NULL", "integer", "NULL"))
}
Cheers,
Josh
On Sun, Jul 18, 2010 at 11:22 AM, LogLord <nils.schoof at web.de> wrote:
>
> Hi,
>
> I have about 300 space-delimited text files and from each file I want to
> import one specific column into R to create a data frame where all imported
> columns are included.
> Is there a smart way to do so?
>
> Thanks!
> --
> View this message in context: http://r.789695.n4.nabble.com/Import-of-specific-column-of-many-space-delimited-text-files-tp2293273p2293273.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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list