How to create column from data contained in other columns

Asked

Viewed 1,516 times

1

Hello, I’m new in R and I’m getting beaten to solve this problem. I have a spreadsheet that I called base and has 2573 observations of 103 variables.

A parte da minha planilha que contém as informações que quero extrair tem a seguinte cara

I created an auxiliary column named after reclamacoes.titulo. I need to check where the 1 is in each row and return, in the column reclamacoes.titulo, or the name of the corresponding column or its index. I was able to write a code that returns the indexes of the rows where each one is. For example, the indexes of the rows that have 1 in the column periculosidade 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 that case, my column reclamacoes.titulo was supposed to have the following face:

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

2 answers

2


The hardest part was made. Now just choose the contents present in dic.busca.indice.periculosidade inside the column reclamacoes.titulo. Just rotate

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

and the desired result will appear.

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

1

I only took columns that have at least one value 1 of your example and also simplified the name of 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

and surrounded a for to take 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)]
}

Browser other questions tagged

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