Replace columns in a data.frame randomly splitted

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace columns in a data.frame randomly splitted

agent dunham
Dear community,

 I'm working with the data.frame attached (df1.xls), let's call it df1.
 I typed:  df1<- read.xls("C:/... dir .../df1.xls",colNames= TRUE, rowNames= TRUE)  

 Then I splited randomly df1 using splitdf function (http://gettinggeneticsdone.blogspot.com/2011/03/splitting-   dataset-revisited-keeping.html)
 
 So now, I have df1 divided in another 2 dataframes: trainset  and testset. (They both have the same row  names and column names as df1)

 I'd like to change df1$v1 and df1$v2 values in the trainset  and testset by the ones in df2 (df2.xls)

 How can I explain R to identify in trainset/testset$v1 and $v2 the rownames  and replace the values by the corresponding ones in df2?

Thanks in advance,

[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Replace columns in a data.frame randomly splitted

Michael Weylandt
Two ways immediately come to mind:

1) Change the values before splitting

2) Use the same seed for the two splits so the rows match exactly and
then just do the changes directly

Any more automated solution will depend on whether your data has
rownames or not.

Michael

PS - As a general rule, most folks don't like to download unknown xls
files -- it's much easier to create a plain text representation of R
data using the dput() command.

On Wed, Nov 30, 2011 at 8:15 AM, agent dunham <[hidden email]> wrote:

> Dear community,
>
>  I'm working with the data.frame attached (
> http://r.789695.n4.nabble.com/file/n4122926/df1.xls df1.xls ), let's call it
> df1.
>  I typed:  df1<- read.xls("C:/... dir .../df1.xls",colNames= TRUE, rowNames=
> TRUE)
>
>  Then I splited randomly df1 using splitdf function
> (http://gettinggeneticsdone.blogspot.com/2011/03/splitting-
> dataset-revisited-keeping.html)
>
>  So now, I have df1 divided in another 2 dataframes: trainset  and testset.
> (They both have the same row  names and column names as df1)
>
>  I'd like to change df1$v1 and df1$v2 values in the trainset  and testset by
> the ones in df2 ( http://r.789695.n4.nabble.com/file/n4122926/df2.xls
> df2.xls )
>
>  How can I explain R to identify in trainset/testset$v1 and $v2 the rownames
> and replace the values by the corresponding ones in df2?
>
> Thanks in advance,
>
> [hidden email]
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Replace-columns-in-a-data-frame-randomly-splitted-tp4122926p4122926.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Replace columns in a data.frame randomly splitted

agent dunham


The thing is that I've already been working with df1, and I was looking for a function that could replace values knowing rows and columns. Does it exist?

Thank you, [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Replace columns in a data.frame randomly splitted

Michael Weylandt
I repeat myself:

Any more automated solution will depend on whether your data has
rownames or not. [...] create a plain text representation of R data
using the dput() command.

Another way that might make more sense is to cbind() the data you need
later on before the split and then it will be carried through.

Michael

On Thu, Dec 1, 2011 at 2:54 AM, agent dunham <[hidden email]> wrote:

>
>
> The thing is that I've already been working with df1, and I was looking for
> a function that could replace values knowing rows and columns. Does it
> exist?
>
> Thank you, [hidden email]
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Replace-columns-in-a-data-frame-randomly-splitted-tp4122926p4127405.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.