r - Changing column names and values -


given following data frame:

> df      a1 a2 a2.1 a2.2 a3 b1 b2 b2.1 b2.2 b3 [1,]  0  0    0    0  0  0  0    0    0  0 [2,]  0  0    0    0  0  0  0    0    0  0 

i seek replace "2.1" column names " aa" , replace values columns number 1. that:

     a1 a2 aa a2.2 a3 b1 b2 b aa b2.2 b3 [1,]  0  0    1    0  0  0  0    1    0  0 [2,]  0  0    1    0  0  0  0    1    0  0 

how can achieve this?

many in advance.

you can try grep , gsub

x = c(0,3,5) y = c(4,1,7) z = c(1,2,3) df = data.frame(x,y, z) names(df) = c("a1","a2.1", "a2.1")  index <- grep("2.1",colnames(df)) df[, index] <- 1 colnames(df) <- gsub("2.1", "aa", colnames(df))  # > df # a1 aaa aaa # 1  0   1   1 # 2  3   1   1 # 3  5   1   1 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -