1

I have a 5 dataframes containing multiple variables (110) in three different languages and I'm pretty new to using R. I'm recoding the factors to numbers that I can merge all of the dataframes in the end. With most of the factors it simply worked, except for the following sentence. I suspect that the dot in "ESG Art. 383 und Art. 384" are confusing but I can't get rid of it

data$B1aC <- as.factor(data$B1aC)
levels(data$B1aC)
summary(data$B1aC)
data$B1aC <- factor(data$B1aC, levels = c("Einsatz auf Wunsch des 
Bewohners/der Bewohnerin oder im Einverständnis mit dem/der dazu 
urteilsfähigen Bewohner/-in","Einsatz bei dazu nicht urteilsfähiger Bewohner/-in, alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt","Kontext ist noch nicht geklärt, nicht alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt"),labels = c("1", "2", "3"))
table(data$B1aC)

When I display the transformed data, I lose numbers 2 and 3 (both having a dot in their level). Does anyone know what I can do?

I'm using Rstudio on Apple (x86_64-apple-darwin13.4.0) with R 3.3.3 running.

this is the output for table()

table(data$B1aC)

Einsatz auf Wunsch des Bewohners/der Bewohnerin oder im Einverständnis mit dem/der dazu urteilsfähigen Bewohner/-in 
                                                                                                                  1 
      Einsatz bei dazu nicht urteilsfähiger Bewohner/-in, alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt 
                                                                                                                  1 
                    Kontext ist noch nicht geklärt, nicht alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt 
                                                                                                                  1 

summary(data$B1aC)
Einsatz auf Wunsch des Bewohners/der Bewohnerin oder im Einverständnis mit dem/der dazu urteilsfähigen Bewohner/-in 
                                                                                                                  1 
      Einsatz bei dazu nicht urteilsfähiger Bewohner/-in, alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt 
                                                                                                                  1 
                    Kontext ist noch nicht geklärt, nicht alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt 
                                                                                                                  1 
                                                                                                               NA's 
                                                                                                                 97 

I had to convert the strings to numbers because the data frames are in 3 different languages - merging then would confuse me, because I'm not too familiar with all the languages.

after transforming the data:

data$B1aC <- factor(data$B1aC, levels = c("Einsatz auf Wunsch des Bewohners/der Bewohnerin oder im Einverständnis mit dem/der dazu urteilsfähigen Bewohner/-in",
                                                    "Einsatz bei dazu nicht urteilsfähiger Bewohner/-in, alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt",
                                                    "Kontext ist noch nicht geklärt, nicht alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt"),
                         labels = c("1", "2", "3"))
table(data$B1aC)

1 2 3 
1 0 0 
divibisan
  • 11,659
  • 11
  • 40
  • 58
tobin_lab
  • 185
  • 1
  • 14
  • "I'm recoding the factors to numbers that I can merge all of the dataframes in the end." - this isn't necessary because you can merge on character variables. Can you describe what you mean by merging here, and include your output? – effel May 22 '17 at 12:35
  • 1
    have you tried `levels(data$B1aC) <- c("Einsatz....","Einsatz...","Kontext...")` ? – Bea May 22 '17 at 12:37
  • I believe the parentheses need to be escaped, thus "\\\(" and "\\\)". – amonk May 22 '17 at 13:07
  • as well as all the punctuation – amonk May 22 '17 at 13:36
  • Please show the output from `table()`. Escaping punctuation or parentheses shouldn't be necessary because `levels()` involves no pattern matching. The vector assigned is "a vector of character strings with length at least the number of levels of `x`, or a named list specifying how to rename the levels." – effel May 22 '17 at 13:49
  • Thank you for all your help!! I added the table output but I didn't come forward with any of the suggestions. I can't merge the character variables because they are in 3 different languages (german, french & italian), thats why I switch to numbers to combine the answers. I can't merge at the moment, because I don't have all the data yet - I started by cleaning/preparing this dataset (which is 1 of 5) and got the problem with the punctuation. I don't get any error msg but I loose two cases.. – tobin_lab May 24 '17 at 07:11

2 Answers2

1

Given your text you can get rid of the punctuations as follows:

  text<-c("Einsatz auf Wunsch des Bewohners/der Bewohnerin oder im Einverständnis mit dem/der dazu urteilsfähigen Bewohner/-in","Einsatz bei dazu nicht urteilsfähiger Bewohner/-in, alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt","Kontext ist noch nicht geklärt, nicht alle Bedingungen (ESG Art. 383 und Art. 384) sind erfüllt")

  text<-gsub(pattern = "[[:punct:]]"," ",text,perl=T)

Thus yielding:

> text

[1] "Einsatz auf Wunsch des Bewohners der Bewohnerin oder im Einverständnis mit dem der dazu urteilsfähigen Bewohner  in"
[2] "Einsatz bei dazu nicht urteilsfähiger Bewohner  in  alle Bedingungen  ESG Art  383 und Art  384  sind erfüllt"      
[3] "Kontext ist noch nicht geklärt  nicht alle Bedingungen   ESG Art  383 und Art  384   sind erfüllt"
amonk
  • 1,769
  • 2
  • 18
  • 27
0

It looks as if you're trying to relabel your factor so that each level has a numeric label, instead of the existing text label. This does not require carefully replicating each of the existing labels, or clobbering their punctuation toward this end.

Factors inherit from integer and as.numeric gives you their numeric representation:

data(warpbreaks)
table(warpbreaks$wool)
# 
#  A  B 
# 27 27 
table(as.numeric(warpbreaks$wool))
# 
#  1  2 
# 27 27 

At this point, if you really wanted, you could relabel the factor numerically (below), but from your mention of needing to merge dataframes by this variable, I can't think of why this would be desirable.

warpbreaks$wool <- factor(warpbreaks$wool, labels =
  unique(as.numeric(warpbreaks$wool)))

table(warpbreaks$wool)
# 
#  1  2 
# 27 27 
effel
  • 1,421
  • 1
  • 9
  • 17