How to create column from data contained in other columns

1

Hello, I'm new to R and I'm getting caught up in solving this problem. I have a spreadsheet I called base and it has 2573 observations of 103 variables.

I've created an auxiliary column named reclamacoes.titulo . I need to check where 1 is on each row and return, in the reclamacoes.titulo column, or the corresponding column name or its index. I was able to write a code that returns the indexes of the lines where each one is. For example, the indexes of the rows that have 1 in the periculosidade column is:

> dic.busca.indice.periculosidade
[1]   81   84   85   91   92  516  575  576  577  578  579  636  637  638  639  640  641  643  742  743  744  745  746  747  969
[26]  970  971  972 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1411 1412 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
[51] 1425 1426 1531 1532 1533 1534 1535 1694 1695 1979

In this case, my column reclamacoes.titulo was supposed to look like this:

Justiça Gratuita
Justiça Gratuita
Perdas e Danos
Perdas e Danos
Perdas e Danos
Perdas e Danos
Perdas e Danos
Prescrição
Perdas e Danos
Justiça Gratuita
Perdas e Danos
    
asked by anonymous 25.10.2017 / 19:29

2 answers

2

The hardest part was made. Now just choose the indexes present in dic.busca.indice.periculosidade within the reclamacoes.titulo column. Just roll

base$reclamacoes.titulo[dic.busca.indice.periculosidade]

and the desired result will appear.

Brackets ( [] ) allows you to search for the i-th observation within a R vector. For example, base$reclamacoes.titulo[12] will return the twelfth observation within base$reclamacoes.titulo .

    
25.10.2017 / 19:42
1

I just took the columns that have at least a 1 value from your example and also simplified the name of the variables to use in my code.

base <- data.frame('Presc' = c(rep(0,7),1,rep(0,3)), 
  'Per_Dan' = c(rep(0,2),rep(1,5),rep(c(0,1),2)), 
  'Just_Grat' = c(rep(1,2),rep(0,7),1,0))

I created the variable reclamacoes.titulo

base$reclamacoes.titulo <- NA

I ran a for to get the name of the column that has the value 1 for each line

for(i in 1:nrow(base)){
  base$reclamacoes.titulo[i] <- names(base)[which(base[i,] == 1)]
}
    
25.10.2017 / 19:55