-1

I am new to the beautiful language of R and currently I am having difficulties understanding the following:

I am loading in a CSV file. This CSV file contains two columns. The first column is a unique ID. The second column is named 'content'. The content column is basically all the readable text from a specific URL.

If I print the content of the first row of the second column I get the following output.

Text[1:1,2:2]
[1] Autoservice Hofra  Home \xa0 Home Contact \xa0 Autoservice Hofra \xa0 Home Contact Autoservice Hofra Welkom op onze website \xa0 \xa0LASSA BANDEN DEALER \xa0 Wanneer is mijn auto toe aan een onderhouds inspectie Iedere auto heeft een instructieboekje Dat ligt meestal in het dashboardkastje van uw auto In dat boekje staat hoe en wanneer u de auto onderhoudt Autoservice Hofra Albert Einsteinweg 12 6045 GX Roermond Tel 0654965305 Kleine en grote beurt Bij Autoservice Hofra kun je terecht voor een grote en kleine beurt voor de zomer en winter checks maar ook airco onderhoud Zo zorgen we dat je niet alleen veilig en comfortabel de weg op gaat maar ook nog eens extra lang plezier hebt van je auto \xa0 We vervangen motorolie en het oliefilter vullen de ruitenvloeistof koelvloeistof en remvloeistof bij en houden rekening met de milieutoeslag Een groot deel van de checks die bij een APK worden gedaan komen ook bij de grote beurt aan bod Het is daarom voordelig om deze te combineren De motormanagement van de auto wordt zorgvuldig uitgelezen met diagnose apparatuur waardoor storingen en andere meldingen aan het licht komen \xa0 \xa0 \xa0 Airco Check \xa0Houdt de airco uw auto op de juiste temperatuur Ontwasemt de airco de autoruit slecht En blaast hij nog frisse lucht Doe de Airco Check Ook als het geen zomer is Wij voeren alle voorkomende onderhoudswerkzaamheden uit aan uw auto voor een zeer scherpe prijs Bij een duurdere reparatie wordt er te allen tijde overleg gepleegd met u Daarnaast zijn we in het bezit van de juiste kennis en gereedschappen om alle voorkomende reparaties uit te voeren \xa0\xa0Laat de werkzaamheden uitvoeren wanneer u dat uitkomt Home \xa0 \xa0 Contact
5432 Levels:  ...

If I print the type of this first row of the second column, I get the type integer

typeof(Text[1:1,2:2])
[1] "integer"

Could someone explain me why specific column is having a type 'integer' while it is containing characters. I am also confused about the '5432 levels: ...' part. What is meant with 'levels' in R?

Finally, what I want to do is convert each row of the 'content' column into characters so I can apply for example remove all rows which have a length of < 200, my code:

SText <- subset(Text, nchar(as.character(content)) > 200)

Right now I get the error:

Error in type(Text[1:1, 2:2]) : could not find function "type"

How can I solve this problem? Any help and information would be highly appreciated!

nielsvg
  • 25
  • 5
  • 2
    The column has been imported as a factor and not characters. You can explicitly tell R to not treat character columns as factors by specifying the stringsAsFactors parameter in the `read.csv()` function. The integer type is most likely due to the fact that the column is stored as factor. – SmitM Nov 02 '18 at 20:47
  • 1
    If you are new to R, start using `class()` rather than `typeof()`. The latter doesn't tell you how objects behave. – MrFlick Nov 02 '18 at 21:01
  • Here's some more background on factors, which took me a while to understand. For many situations, using `stringsAsFactors = FALSE` will be simpler to work with. Down the road, factors can be very useful, for instance if you want to sort the categories of a plot (and not in alphabetical order). https://www.stat.berkeley.edu/~s133/factors.html – Jon Spring Nov 02 '18 at 21:30
  • To add to other answers here. You can also avoid this sometimes by specifying `na.strings` when you import data. – Brandon Bertelsen Nov 03 '18 at 00:30

2 Answers2

0

In the background, and for efficiency reasons, factors are stored as numbers. And by default, read.csv() reads characters columns as factors (which makes sense in a statistical world), hence the fact that it is an integer. You should use stringsAsFactors = FALSE on read.csv() to get a character vector (not a factor).

The 5432 levels: ... refers to the levels of the factor contained in this column. A factor always keeps track of ALL its original possible levels. So even if you just extract one element, it will still have all the information about the level.

typeof() is not the function you should use if you need to know the content of an object. As stated in the doc, typeof() returns the internal representation of the object. Hence the integer you get.

You should try class() instead.

Colin FAY
  • 3,744
  • 1
  • 8
  • 24
0
  1. read your csv by specifying stringsAsFactors=FALSE

    DF <- read.csv("/Users/mypath/finalTotal.csv", stringsAsFactors=FALSE) DF2 <- read.csv("/Users/mypath/model.csv", stringsAsFactors=FALSE)

  2. \xa0 is a non-breaking space in Latin1 (ISO 8859-1) in your DFText. You should replace it with a space.

    DFText[,2] = gsub("\xa0", " ", DFText[,2])

Bing
  • 935
  • 1
  • 7
  • 19