Modify the content of a dataframe in R

I would like to transform/modify the content of dataframe. Basically I have a dataframe like below:

        bins      pval
1   2L:1:150 0.9224217
2 2L:151:300 0.9478824
3 2L:301:450 0.9671139
4 2L:451:600 0.9280847
5 2L:601:750 0.9698584
6 2L:751:900 0.9725379

And I would like to transform/modify into another dataframe like this, where I split the content of my "bins" column (first row) into 150 rows containing the same values. And so on for the second row.

    chr  pos    pval
1   2L   1 0.9224217
2   2L   2 0.9224217
3   2L   3 0.9224217
4   2L   4 0.9224217
5   2L   5 0.9224217
...
150  2L   150 0.9224217
151  2L   151 0.9478824
152  2L   152 0.9478824
153  2L   153 0.9478824
etc...

Any help much appreciated,

Ben

Answers


The quick answer which may be, I fear, too specific and may need generalization. Assume the first dataframe is named "df1":

data.frame(chr="2L", pos=1:(150*NROW(df1)), pval=rep(df1$pval, each=150) )

Argument recycling should make the "chr" long enough without a rep function.

Edit in reply to comment. If the repeat length is always 150 then the fix is easy:

data.frame(chr = rep(substr(df1$bins, 1,2), each=150), 
           pos = 1:(150*NROW(df1)), 
           pval = rep(df1$pval, each=150) )

Need Your Help

Salesforce SOAP vs REST

c# .net salesforce

I have been building a console app the uses the Saleforce SOAP API, and now need to use the Salesforce API in a web app.

.on('click') in for loop to multiple elements?

javascript jquery

I'm having trouble with my function. I need a for loop to attach a click event to my elements. Now it sadly only adds the click event to the last element.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.