Fornax
Fornax

Reputation: 47

Column names change with a counter included

I need to change the column names from a data frame.

I'm aware of some methods to do this. For instance: colnames(allFeatures)[1] <- "driver" I want to change the number of columns but besides include inside a sequence of numbers. For instance the probabilities used in a distribution: SO I want.

First column: driver

Second column: Speedistribution 0,05

Third Column: SpeedDistirbution 0,1

Fourth Column: SpeedDistirbution 0,15

I have tried applying some loops but do not work because I do not know how to include it on the header.

I have tried:

i=3
      j=0.05
      while(i<23){
      colnames(allFeatures)[i] <- c( "SpeedDistribution %", j)
      i=i+1
      j=j+0.05
      }

I will be pleased if someone would help me. Thanks in advance

Upvotes: 0

Views: 57

Answers (1)

lukeA
lukeA

Reputation: 54237

Try

colnames(allFeatures)[3:22] <- paste("SpeedDistribution %", cumsum(rep(0.05, 19)))

Upvotes: 2

Related Questions