[R] howto get the number of columns and column names of multiply data frames
Don MacQueen
macq at llnl.gov
Sun Aug 9 22:26:36 CEST 2009
## You can use get()
for ( i in 1:n) {
nm <- paste('dataframe',i,sep='')
cat( ncol( get(nm)), 'columns in',nm,'\n') )
}
## or
nms <- ls(pattern='dataframe')
for (nm in nms) cat( ncol(get(nm)) , 'columns in',nm,'\n') )
}
(Assuming I have balanced parantheses, that is --
my email software doesn't check that like Emacs
does!)
Storing the dataframes as elements of a list, as
Steve Lianoglou suggested, lets you avoid using
the get() function.
You could also use the count.fields() function to
check whether the files have the correct number
of columns even before you read the data it. Or
make a pass through the files reading in only the
first line as data, and comparing those as data
rather than as a names attribute of a dataframe.
-Don
At 5:29 PM +0200 8/9/09, Frank Schäffer wrote:
>Hi,
>I' ve read in several files with measurements into R data frames(works
>flawlessly). Each dataframe is named by the location of measurement and
>contains hundreds of rows and about 50 columns like this
>
>dataframe1.
>date measurment_1 .... mesurement_n
>1
>2
>3
>..
>..
>..
>n
>
>For further processing I need to check whether or not ncol and colnames are
>the same for all dataframes.
>Also I need to add a new column to each dataframe with contain the name of the
>dataframe, so that this column can be treated as factor in later processing
>(after merging some seleted dataframes to one)
>
>I tried out
>
>for (i in 1:length(ls()){
> print(ncol(ls()[i])
>}
>
>but this does not work because r returns a "character" for i and therefore
>"NULL" as result.
>Reading the output of ls() into a list also does not work.
>
>How can I accomplish this task??
>
>Best regards and thanks
>
>Frank
>
>______________________________________________
>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.
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
More information about the R-help
mailing list