0

Ok so I have a large data frame. A small sample of it looks like this. There are extra columns that aren't here including a date column. (I'm sorry I can't format it nicely)

row.names   Vic.Arts.Centre Bourke.St.Russell.St..West. Chinatown.Lt.Bourke.St..South.  State.Library   Bourke.Street.Mall..South.  Collins.Place..South.   Flinders.St.Station.Underpass   Melbourne.Central   Convention.Exhibition.Centre    Lygon.St..West. Waterfront.City Flinders.St.Elizabeth.St..East. Birrarung.Marr  Collins.Place..North.   Lonsdale.St..South. Spencer.St.Collins.St..South.   QV.Market.Peel.St   Australia.on.Collins    Sandridge.Bridge    Southern.Cross.Station  Spencer.St.Collins.St..North.   Hour    Flagstaff.Station   Princes.Bridge  Town.Hall..West.    Webb.Bridge Alfred.Place    Bourke.Street.Mall..North.  Chinatown.Swanston.St..North.   New.Quay    Flinders.St.Spark.Lane  Flinders.St.Spring.St..West.    Victoria.Point  QV.Market.Elizabeth..West.
1   1465    74  305 123 145 89  51  237 452 0   136 49  283 0   14  202 41  30  0   171 7   74  0   38  280 287 8   47  0   154 35  35  22  20  97
2   1466    46  159 32  97  40  21  102 138 0   68  16  109 0   13  180 16  16  0   60  12  30  1   9   78  146 7   15  0   58  16  8   1   1   69
3   1467    25  88  15  23  43  4   63  73  0   39  2   52  0   4   77  11  5   0   33  2   22  2   15  48  49  1   7   0   31  8   5   1   4   46
4   1468    35  77  12  17  27  6   34  22  0   17  0   49  0   2   45  1   8   0   13  2   8   3   7   52  49  0   13  0   17  2   6   3   3   44

My problem is that not all of these values are numbers. Some of the columns are actually of type character and when I try to sum the columns and rows it fails.

I can change the data type of a column by just using as.integer but is there a way that I can iterate through the columns, check its data type, then if the data type is character change it without having to do it all manually?

Frank
  • 66,179
  • 8
  • 96
  • 180
user2787386
  • 303
  • 2
  • 7
  • 20
  • Converting character to integer doesn't make much sense; you'll just get `NA` values. Maybe you just want to select the right columns to sum? Like `mycols <- sapply(mydf,is.integer); rowSums(mydf[,mycols])` – Frank May 01 '15 at 14:59
  • 2
    @Frank I think op means some of the columns are `c('1','2','3','4')`, wants to convert that to `1:4`, and sum – rawr May 01 '15 at 15:03
  • I mean that some of the columns are characters when they should be integers. I can manually go through and cast them as integers but that takes a great deal of time. – user2787386 May 01 '15 at 15:32

0 Answers0