How to insert a marker that grows numerically into a dataframe in R?

Asked

Viewed 40 times

0

I have a dataframe with a column and a certain number of rows that varies. What I’m working on is 17. On each line there is a different expression, more or less similar to "ID 123456, color = blue".

I would like a word to be added to each line to help me identify each of these lines. I would need something like cod1, cod2, cod3 etc. It turns out that as I said before, the amount of lines changes regularly. So in this case it’s 17 lines, but it could be more or less. Therefore, I would need the word that will identify each of these lines to match the number of lines.

  • The question is a little too generic. Edit the question by placing the original 17-line data frame. Use the output of the dput(dataframe) command for this. Also put the expected result of this counter, so that we understand your goal.

  • Hey, @Marcusnunes, thanks for the tip, I’m a beginner in all this. After good research, I managed to evolve in this issue, but already appeared another, however, I do not know how to proceed, because it is the first time I use the forum - creating a question.

1 answer

0

Hello,

You can make one like this:

for(j in 1:dim(seu dataframe)[1]){
    seu dataframe[j,]$posicao <- paste0("cod",j)
    j=j+1
  }

this function will concatenate the word Cod with the counter... will be cod1 in the first line, cod2 in the second and so on.

  • Thank you so much! It worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.