Hi, @garage First of all, it is going to be easier for folks around here to help if you include the data and code you've used to get to where you are stuck rather than just a table. See this post for more information about best practices for writing questions and including a reproducible example (reprex).
If I'm understanding what you are looking to do, you want to multiple all values in columns between V1
and score
by -1
. (Although the expected output you included is not exactly that. If this is not what you want to do, could you explain in a little more detail?) One approach is to use the mutate_at()
function from dplyr
which allows you to modify several columns in the same way. First, I select the columns I want to modify within the vars()
function, and then I define an anonymous function to operate on each of the columns.
library(tidyverse)
df <- tibble::tribble(
~V1, ~V2, ~V3, ~V4, ~V5, ~V6, ~V7, ~V8, ~V9, ~score, ~Name, ~ID,
1, 0, 0, 0, 0, 0, 0, 0, 0, 5, "AA", 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 5, "AA", 2,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "CC", 3,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "CC", 4,
0, 0, 1, 0, 0, 0, 0, 0, 0, 10, "EE", 5,
0, 0, 1, 0, 0, 0, 0, 0, 0, 10, "EE", 6,
0, 0, 0, 1, 0, 0, 0, 0, 0, 15, "GG", 7,
0, 0, 0, 1, 0, 0, 0, 0, 0, 15, "GG", 8,
0, 0, 0, 0, 1, 0, 0, 0, 0, 5, "HH", 9,
0, 0, 0, 0, 1, 0, 0, 0, 0, 5, "HH", 10,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "KK", 11,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "KK", 12
)
df %>%
mutate_at(vars(V1:score), ~(.x * -1))
#> # A tibble: 12 x 12
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 score Name ID
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
#> 1 -1 0 0 0 0 0 0 0 0 -5 AA 1
#> 2 -1 0 0 0 0 0 0 0 0 -5 AA 2
#> 3 0 -1 0 0 0 0 0 0 0 0 CC 3
#> 4 0 -1 0 0 0 0 0 0 0 0 CC 4
#> 5 0 0 -1 0 0 0 0 0 0 -10 EE 5
#> 6 0 0 -1 0 0 0 0 0 0 -10 EE 6
#> 7 0 0 0 -1 0 0 0 0 0 -15 GG 7
#> 8 0 0 0 -1 0 0 0 0 0 -15 GG 8
#> 9 0 0 0 0 -1 0 0 0 0 -5 HH 9
#> 10 0 0 0 0 -1 0 0 0 0 -5 HH 10
#> 11 0 0 0 0 0 -1 0 0 0 0 KK 11
#> 12 0 0 0 0 0 -1 0 0 0 0 KK 12
Created on 2018-11-01 by the reprex package (v0.2.1)