[R] How to automatically create data frames from an existing one?

Sarah Goslee sarah.goslee at gmail.com
Wed Jan 11 18:31:09 CET 2017


I do not understand why you want to take a perfectly good data frame
and split it into a whole bunch of single-column data frames instead
of working with it as-is. The latter seems like an awkward and
unnecessary thing to do.

If you explain what you're trying to do, we can help. Referencing
MATLAB code isn't useful, because R does not have the same underlying
way of working.

You can readily use columns of a data frame in other operations
without doing this.

Sarah

On Wed, Jan 11, 2017 at 6:53 AM, Tunga Kantarcı <tungakantarci at gmail.com> wrote:
> I have a data frame that includes several columns representing
> variables and variables names are indicated at the top row of the data
> frame. That is, I had a csv file where variable names were stored in
> the top row, and when I imported the csv file to R, R created a data
> frame that appears with the name rwrdatafile (custom name I gave)
> where I can see all the variables with their names on the top row in
> RStudio. For example, one of the columns stores wage data and I can
> create a stand alone data frame (shall I call it a vector data frame?)
> for wage, but do this for all variables.
>
> That is, I can execute the command
>
> wage = rwrdatafile[,1,drop=FALSE]
>
> which nicely creates wage and RStudio shows it as data in its
> environment window and if I click on it, I can inspect it in a spread
> sheet like view and work with that data say in regression analysis.
>
> The problem is that there are many variables stored in the data frame
> rwrdatafile, and it is very tedious to repeat the above mentioned
> routine for each variable. Hence I attempted to write a for loop for
> this but it helped to no avail.
>
> In particular, I tried
>
> for (i in 1:k){
>   assign(names(rwrdatafile)[i],rwrdatafile[,i])
> }
>
> and in fact this nicely assigns each column in the data frame to a
> name, but I do not see the variables as data in the environment
> section. But what I need are variables that I can work with in matrix
> operations.
>
> I also tried
>
> for(i in 1:k){
>   names(rwrdatafile)[i] = rwrdatafile[,i,drop=FALSE]
> }
>
> thinking that this for loop would just repeat what I do for
>
> wage = rwrdatafile[,1,drop=FALSE]
>
> for all the variables in rwrdatafile.
>
> Please note that I do need to use a for loop and in fact I need to
> translate and imitate the MATLAB code below, which does the job in
> MATLAB, as close as possible in R.
>
> # MATLAB code generating variables from structure array rwrdatafile
> [N,k] = size(rwrdatafile.data);
> for i = 1:k
>     eval([cell2mat(rwrdatafile.textdata(i)) '= rwrdatafile.data(:,i);'])
> end
>
-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list