I have a dataframe df and the column names are 1, 10, 20, 30.
I perform an interpolation which expands df by adding one column in between each existing columns (it is the midpoint). Doing this destroys column names.
What would be a formula/method to re-assign column names here? I should be getting:
1, 5, 10, 15, 20, 25, 30.
df is commonly used as a name for at data.frame; however, it is also the name of a built-in function. dat avoids the possible treatment of the label as a closure.
Column names should be character, not numeric.
It is not just a matter of assigning column names, there must also be values for the rows, and the number of rows in the existing data frame sets the number of rows required in each new column.
To create a list of interpolated names for this case
Thanks this is very informative. I wanted to try and make my question as simple as possible for the purpose of example, but I also did it to benefit my own understanding.
My "real world" problem is that df is a 50x50 grid (I won't use the term 'matrix' since that's a different object type) where the row names and column names are the same. I should add that the names are all decimal-containing (10.25, 20.50, etc).
In this case if my interpolation value is 1 (to add a single column between existing columns which is the midpoint) the resulting data frame will have 99 columns, and 99 rows.
I'm looking for a way to get what those values are. If I need to do a paste0 function and prepend some text to each as a way of using R correctly, I can do that.
My function will interpolate 2 values in between each of these rows. However, as I was mentioning before the tool I'm using will not preserve the names. They are all destroyed. Therefore I know I will end up with:
1 x
2 x
3 x
4 x
5 x
6 x
7 x
8 x
9 x
10 x
How can I go back and assign the original numbers, and then the 2 new values which should the midpoints between those numbers? The correct output would be: