how to fill in gaps of a dataframe, by a given interval?

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.

Thank you!

  1. 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.
  2. Column names should be character, not numeric.
  3. 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.
  4. To create a list of interpolated names for this case
#> [1] "Var5"  "Var15" "Var25"

Created on 2020-09-22 by the reprex package (v0.3.0.9001)

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.

Beginning with a vector of 50 elements, interpolating a value between each of them will yield a vector of 75, not 99 elements.

I can't seem to change the column and row names. Is there a formula I could use to say :

colnames(interpolated_df) <- colnames(df\\\\\formula-here\\\\\\\)

and I'd do the same for row names.

colnames(mtcars) <- paste0(colnames(mtcars),"_newtag")
#>  [1] "mpg_newtag"  "cyl_newtag"  "disp_newtag" "hp_newtag"   "drat_newtag"
#>  [6] "wt_newtag"   "qsec_newtag" "vs_newtag"   "am_newtag"   "gear_newtag"
#> [11] "carb_newtag"

Created on 2020-09-23 by the reprex package (v0.3.0.9001)

mydf <-,4,7,10))
#  c(1, 4, 7, 10)
1              1
2              4
3              7
4             10

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:

1              1
2              2
3              3
4              4
5              5
6              6
7              7
8              8
9              9
10             10

Integers are not names, character vectors are names, even if they are character representations of integers.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.