Diggy Detroit Diggy Detroit - 1 month ago 7
R Question

Read data stored as character

I have a column within a dataframe with numbers that looks like this below

City Temperature
Edmonton, Alberta 4.1,13.6,15.2,15.7,14.2,15.2,16,14.2,17,13.1
Edmonton, Alberta 15,18.2,14.8,16.5,14.6,16.9,14.3,17.5,13,15.8
Edmonton, Alberta 15.8,17.9,16.9,15.1,13.2,13.1,16.8,12.4,14.7,15.6
Edmonton, Alberta 14.3,17.3,14.6,17.3,14.8,14,15.4,14.1,16,15.4


My objective is to read the data in
Temperature
column and create two additional columns that stores the Minimum and Maximum temperatures like this.

City Temperature Min Max
Edmonton, Alberta 4.1,13.6,15.2,15.7,14.2,15.2,16,14.2,17,13.1 4.1 16
Edmonton, Alberta 15,18.2,14.8,16.5,14.6,16.9,14.3,17.5,13,15.8 13 18.2
Edmonton, Alberta 15.8,17.9,16.9,15.1,13.2,13.1,16.8,12.4,14.7,15.6 12.4 17.9
Edmonton, Alberta 14.3,17.3,14.6,17.3,14.8,14,15.4,14.1,16,15.4 14.1 17.3


I tried the simple
min(df$Temperature[1])
function but it didnt work. So not sure how to deal with this data, any advise or suggestion is much appreciated.

42- 42-
Answer

The scan function can read across text fields and parse out values divided by the "sep" argument:

> dat$min_temp <- sapply( as.character(dat$Temperature), 
                    function(x) min( as.numeric( scan( text=x, sep=",", what=""))))
Read 10 items
Read 10 items
Read 10 items
Read 10 items
> dat
              City                                       Temperature
1 Edmonton,Alberta      4.1,13.6,15.2,15.7,14.2,15.2,16,14.2,17,13.1
2 Edmonton,Alberta     15,18.2,14.8,16.5,14.6,16.9,14.3,17.5,13,15.8
3 Edmonton,Alberta 15.8,17.9,16.9,15.1,13.2,13.1,16.8,12.4,14.7,15.6
4 Edmonton,Alberta     14.3,17.3,14.6,17.3,14.8,14,15.4,14.1,16,15.4
  min_temp
1      4.1
2     13.0
3     12.4
4     14.0