Replacing column names using a data frame in r

I have a matrix

m <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE,dimnames = list(c("s1", "s2", "s3"),c("tom", "dick","bob")))

   tom dick bob
s1   1    2   3
s2   4    5   6
s3   7    8   9

#and the data frame

current<-c("tom", "dick","harry","bob")
replacement<-c("x","y","z","b")
df<-data.frame(current,replacement)

  current replacement
1     tom           x
2    dick           y
3   harry           z
4     bob           b

#I need to replace the existing names i.e. df$current with df$replacement if 
#colnames(m) are equal to df$current thereby producing the following matrix


m <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE,dimnames = list(c("s1", "s2", "s3"),c("x", "y","b")))

   x y b
s1 1 2 3
s2 4 5 6
s3 7 8 9

Any tips? Should I use an "if" loop? Thank.

+5
source share
2 answers

You can use whichto match colnamesfrom mwith values ​​in df$current. Then, when you have indexes, you can multiply replaceable names from df$replacement.

colnames(m) = df$replacement[which(df$current %in% colnames(m))]

In the above example:

  • %in%tests for TRUEor FALSEfor any matches between compared objects.
  • which(df$current %in% colnames(m)) identifies indexes (in this case line numbers) of matching names.
  • df$replacement[...]is the main way to subset a column df$replacementthat returns only rows matching step 2.
+7

- match:

> id <- match(colnames(m), df$current)
> id
[1] 1 2 4
> colnames(m) <- df$replacement[id]
> m
   x y b
s1 1 2 3
s2 4 5 6
s3 7 8 9

, %in%, , , , ,

> n <- 50000 # size of full vector
> m <- 10000 # size of subset
> query <- paste("A", sort(sample(1:n, m)))
> names <- paste("A", 1:n)
> all.equal(which(names %in% query), match(query, names))
[1] TRUE
> library(rbenchmark)
> benchmark(which(names %in% query))
                     test replications elapsed relative user.self sys.self user.child sys.child
1 which(names %in% query)          100   0.267        1     0.268        0          0         0
> benchmark(match(query, names))
                 test replications elapsed relative user.self sys.self user.child sys.child
1 match(query, names)          100   0.172        1     0.172        0          0         0
+7

All Articles