val crimeDataWithoutHeaderDF = crimeDataWithoutHeader.
map(rec => {
val r = rec.split(",")(r(7),r(5))
}).toDF(“location_idenfier”,“crime_type”)
error: recursive value r needs type
val r = rec.split(", ")(r(7),r(5))
please help me out from this error
What is line val r = rec.split(",")(r(7),r(5))
? intended to do? Can you give an example of what you expect the value r
to be for some specific rec
?
1 Like
r is a String; pls see the lines below (assuming rec is line line of text from crime data):
scala> var rec=“When,in,disgrace,with,fortune,and,men’s,eyes,I,all,alone,beweep,my,outcast,state”
scala> var r = rec.split(",")
var r: Array[String] = Array(When, in, disgrace, with, fortune, and, men’s, eyes, I, all, alone, beweep, my, outcast, state)
scala> var r2=(r(7),r(5))
var r2: (String, String) = (eyes,and)
so, just do that:
val parts = rec.split(",")
val r = (parts(7), parts(5))
//now what? You probably want to do something with `r`?
He/She probably just want to return (r(7), r(5))
to construct a dataframe of those two columns.
So probably:
val crimeDataWithoutHeaderDF =
crimeDataWithoutHeader.map { rec =>
val r = rec.split(",")
(r(7), r(5))
}.toDF(“location_idenfier”, “crime_type”)
Maybe OP was confused with some oneliner alternative like:
val crimeDataWithoutHeaderDF = crimeDataWithoutHeader.map(rec => { val r = rec.split(","); (r(7), r(5)) }).toDF(“location_idenfier”, “crime_type”)
Note the ;
between the two expressions.
However, IMHO, it is better to avoid that kind of inline blocks, not only for the necessity of adding ;
, but because that makes the code harder to read.