1
votes

Hello I have a data frame that is 2000x56. I would like to do a simple subtraction of specific columns. For example I would like to subtract column 1 from 3 and column 5 from 7 etc..

Here is a sample of the data set.

df= structure(list(c(48.9518, 47.9639, 47.5751, 46.5795, 46.6301, 
45.0705, 43.7893, 43.8325, 46.507, 45.1127, 46.2437, 44.6545, 
43.5113, 43.2287, 43.6998, 41.44, 41.44, 41.8239, 43.2681, 42.5079, 
40.315), c(51.9657, 50.928, 50.559, 50.477, 51.8529, 47.506, 
49.0126, 47.8382, 57.6266, 59.9311, 71.9462, 44.6545, 43.5113, 
43.2287, 43.6998, 41.44, 41.44, 41.7783, 43.6673, 42.915, 40.4284
), c(42.0552, 40.141, 40.07, 40.3302, 39.7687, 39.3804, 40.5853, 
40.2478, 40.7404, 36.0079, 39.3361, 38.6883, 33.1306, 34.2174, 
34.0593, 34.4541, 32.1919, 36.2109, 37.0591, 35.7394, 34.8065
), c(43.5527, 40.6115, 41.1305, 42.6484, 42.1938, 41.2828, 41.8979, 
41.9331, 47.0511, 48.0175, 49.5343, 45.5063, 33.1306, 34.2174, 
34.0593, 34.4541, 32.0264, 36.1705, 37.2596, 35.5938, 34.3885
), c(56.3464, 53.5964, 55.2791, 54.7751, 53.6983, 48.2984, 46.8343, 
50.339, 54.6205, 54.6327, 53.7313, 51.839, 49.9128, 60.1649, 
64.1637, 57.4661, 57.4661, 57.9187, 51.9147, 51.5786, 49.357), 
    c(61.6417, 57.054, 58.8402, 60.6182, 58.3043, 48.7071, 47.5466, 
    52.9527, 67.9061, 64.3576, 63.6387, 61.2588, 43.1908, 59.254, 
    63.8611, 57.4661, 57.4661, 58.6671, 54.097, 53.8527, 51.4929
    ), c(62.3702, 58.9045, 58.1827, 59.4045, 57.7552, 50.4304, 
    45.2969, 51.3944, 55.3861, 54.3857, 50.634, 49.1729, 51.0196, 
    56.8711, 59.2268, 56.1792, 56.812, 53.9583, 52.6343, 49.8832, 
    47.8319)), row.names = c(NA, -21L), class = c("tbl_df", "tbl", 
"data.frame"))

head(df)
A tibble: 6 x 7
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  49.0  52.0  42.1  43.6  56.3  61.6  62.4
2  48.0  50.9  40.1  40.6  53.6  57.1  58.9
3  47.6  50.6  40.1  41.1  55.3  58.8  58.2
4  46.6  50.5  40.3  42.6  54.8  60.6  59.4
5  46.6  51.9  39.8  42.2  53.7  58.3  57.8
6  45.1  47.5  39.4  41.3  48.3  48.7  50.4

I start by creating 2 vectors with the column numbers I would like to subtract.

First = seq(1, ncol(df), 4)
Second = seq(3, ncol(df), 4)
print(First)
1, 5
print(Second)
3, 7

Now I create a loop using map2 from purrr. I would like the output to be a dataframe so I use map2_dfr() from purrr

map2_dfr(First, Second, ~df[,.x]-df[,.y])

The result is a tibble with nothing.

I have tried creating a function inside map2_dfr() with no luck.

map2_dfr(First, Second, function(x, y){df[,x]-df[,y]})

My expected output is a data frame where

Column1 = df[,1]-df[,3]
Column2 = df[,5]-df[,7]

Thank you.

1
you don't have any column names. - akrun
Hey @akrun I thought this might be the problem, but if I do map2() instead of map_dfr(), I still get an empty list back. - Jordan Wrong
I think the structure is faulty for a a tidyverse i.e. without a column name - akrun
do you need a single column or multiple column because the map_dfr it binds columns having the same name into a single column - akrun
Your absolutely right (as usual). Thanks @akrun. Ps. I had colmuns of the same name so it was giving me a combined column - Jordan Wrong

1 Answers

1
votes

The issue is that the dataset doesn't have any column names

colnames(df) <- paste0("col", seq_along(df))

Now, applying the OP's code should work fine