Is there a way to replace the 0 of one column with values of another column in the dataframe in R?

Asked

Viewed 19 times

1

I made a merge of 2 dataframes and I realized later that I had 2 columns that have the same information. As in the example below:

linha qt_funcio nu_funcio ...
1     0         3
2     0         4
3     0         1
4     2         0
5     4         0
6     1         0

I wanted to put it all together for nu_funcio and delete the qt_funcio column without doing mutatis or add the columns. Any suggestions?

1 answer

0


A logical vector is created to index the elements of nu_funcio zero. E uses this vector to assign the values of qt_funcio. Then just remove the extra column.

i <- dados$nu_funcio == 0
dados$nu_funcio[i] <- dados$qt_funcio[i]
dados$qt_funcio <- NULL

dados
#  linha nu_funcio
#1     1         3
#2     2         4
#3     3         1
#4     4         2
#5     5         4
#6     6         1

Data in format dput.

dados <-
structure(list(linha = 1:6, qt_funcio = c(0L, 0L, 0L, 2L, 4L, 1L), 
nu_funcio = c(3L, 4L, 1L, 0L, 0L, 0L)), class = "data.frame", 
row.names = c(NA, -6L))

Browser other questions tagged

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