'What are these codes doing? (paste function,setnames)

I am confused about the following codes are doing:

X2_X26 <- paste("X", 2:26, sep = "")

portf_exret <- paste("excess_return_portfolio", 1:25, sep ="")

X27_X51 <- paste("X", 27:51, sep = "")

logsize_p <- paste("logsize_portfolio", 1:25, sep = "")

setnames(datafile, old = 'X1', new = 'market_exret')

setnames(datafile, old = X2_X26, new = portf_exret)

setnames(datafile, old = X27_X51, new = logsize_p)
  1. For the first line, is it saying: create X2,X3...X26(each of them are seperate columns), and then store it into a dataframe called"X2_X26"? Then, the setnames function says change the name of X2_X26 dataframe to portf_exret dataframe, nothing else change?

  2. As we have not previously defined 'X1', for the setnames(datafile, old='X1), is it refering to the first column in the dataframe by default?

  3. What are these code doing? Why we need to change the column from 2:26 to 1:25?

Thank you very much for your help.



Solution 1:[1]

  • datafile is frame of class data.table
  • it has columns named "X1", "X2", "X3"... "X51" (and possibly additional columns)
  • The lines X2_X26 <- paste("X", 2:26, sep = "") and X27_X51 <- paste("X", 27:51, sep = "") are creating vectors, each of length 25, containing the strings "X2", "X2"..."X26" and "X27", "X28", ... "X51", respectively
  • The line assigning to portf_xret is creating a string vector of length 25, that will eventually replace the first set of generic columns X2 through X26. This string vector looks like this "excess_return_portfolio1", "excess_return_porfolio2" ... "excess_return_portfolio25"
  • The line assigning to logsize_p is creating a string vector of length 25, that will eventually replace the second set of generic columns X27 through X51. This string vector looks like this "logsize_portfolio1", "logsize_portfolio2" ... "logsize_portfolio25"
  • Finally, data.table::setnames() is called three times, each time feeding datafile (the object for which the columns should be renamed), the old names, and the new names. These three lines could also have been combined like this: setnames(datafile, old=c("X1", X2_X26, X27_X51), new=c("market_exret", portf_xret, logsize_p))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 langtang