Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:37:52+00:00 2026-06-06T06:37:52+00:00

I am looking for some ideas/functions to improve a task I coded in a

  • 0

I am looking for some ideas/functions to improve a task I coded in a quite inefficient way.

My originial data frame look like:

        CUSIP       Date        Pclose  TRI
1       30161N101   2011-01-03  38.8581 2011-01-03
2       06738G878   2011-01-03  48.4040 2011-01-03
3       74339G101   2011-01-03  24.0880 2011-01-03
4       74348A590   2011-01-03  81.7200 2011-01-03
5       26922W109   2011-01-03  87.8700 2011-01-03
...

The column ‘TRI’ contains the Date in a date format.

And what I want to obtain look like:

    Date        233052109   126650100   251566105   149123101 ...
1   2011-01-03  22.8031     34.3034     11.2645      91.6178
2   2011-01-04  22.6843     34.2740     11.1862      91.1897
3   2011-01-05  22.7933     34.6362     10.9948      91.9779
4   2011-01-06  22.8034     34.2838     11.0470      91.0242
5   2011-01-07  22.6248     34.3034     11.0644      91.2091
.
.
.

In the second data frame each column (except the date one) has the name of a CUSIP from the first data frame and is filled with data from Pclose.

I am making my second data frame with regular loop but I am sure there is a way with compilled function do to way better (perhaps subset)

my functions are:

To build the second data frame:

function(cusippresent,cusiplist){
    workinglist=list()
    workinglist[1]=as.character('Date')
    position = 2
    for (i in 2:length(cusippresent)) {
        if(cusippresent[i] %in% cusiplist) {
            workinglist[position]=as.character(cusippresent[i]);
            position=position+1
        }
    }

    rm(position)

    Data=data.frame()

    #On remplit la première ligne du dataframe
    #avec des éléments du bon type sinon il y a des problèmes

    Data[1,1]=as.character('1987-11-12')

    Data[1,2]=as.numeric(1)
    for (i in 2:length(workinglist)){Data[1,i]=as.numeric(1)}
    for (i in 1:length(workinglist)){colnames(Data)[i]=workinglist[i]}

    return(Data)    
}

To fill the data frame:

function(DATA11C,TCusipC){
    nloop = 1
    positionorigine = 1
    positioncible = 1

    #copy of dates
    datelist = DATA11C[,"Date"]
    datelist = unique(datelist)
    for (i in 1:length(datelist)) {
        TCusipC[i,"Date"]=as.character(datelist[i])
    }

    #creation of needed columns
    TCusipC[,ncol(TCusipC)+1] = as.Date(TCusipC[,"Date"])
    colnames(TCusipC)[ncol(TCusipC)] = 'TRI'

    #ordering of tables
    DATA11C=DATA11C[with(DATA11C,order(TRI)),]
    TCusipC=TCusipC[with(TCusipC,order(TRI)),]

    longueur = nrow(TCusipC)

    #filling of the table 
    while(nloop<longueur) {
        while(DATA11C[positionorigine,"TRI"]==TCusipC[positioncible,"TRI"]){
            nom = as.character(DATA11C[positionorigine,"CUSIP"]);
            TCusipC[positioncible,nom]=as.numeric(DATA11C[positionorigine,"Pclose"]);
            positionorigine=positionorigine+1
        };
        nloop=nloop+1;
        positioncible=positioncible+1
    }

    return(TCusipC)
}

Any suggestion on what function to dig up? potential improvement?

Many thanks,

Vincent

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:37:55+00:00Added an answer on June 6, 2026 at 6:37 am

    That is exactly what the reshape packages does

    library(reshape)
    cast(Date ~ CUSIP, data = DATA11C, value = "Pclose")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing a weird error message and am looking for some ideas as to
Looking for some advice on the best way to implement localization along with client
I'm looking for a way to wrap stack allocations in abstract data types. For
I'm looking for some pointers here as I don't quite know where to start
I'm looking for some Best Practices for automating the deployment of Stored Procedures/Views/Functions/Table changes
Looking for some ideas here... I have a MySQL table that has 100K rows
I'm looking for some ideas on implementing a basic message factory that reads a
Some good controls and examples to chart functions I am looking for some examples
I'm fiddling with a bit of c++/opencv. I was looking some of the samples
Looking at some assembly code for x86_64 on my Mac, I see the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.