[R] sum some columns for each row
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Wed Jul 15 00:48:08 CEST 2015
I suspect your data frame "dat" has non-numeric data in some of the columns that have ABC in their names. Any column of a data frame can be numeric or not, but the data frame as a unit cannot be numeric. If your data file has odd characters in done of the otherwise-numeric columns, the whole column will be read in as a factor or character strings. Look at the output of str(dat) for columns that don't show "num'. If you can find the column, and then one of the bad rows, you can use a text editor to fix them manually, or show us examples of the bad data and we can suggest ways to fix it in R.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On July 14, 2015 2:35:38 PM PDT, Dawn <dawn1313 at gmail.com> wrote:
>Hi,
>
>I used a small set of data (several columns and rows) and it works fine
>using the following command:
>abc <- rowSums(test[,grep("ABC",names(test),fixed=T)],na.rm=T)
>
>But when I used the real big data table, "Error in rowSums(dat[,
>grep("ABC", names(dat), fixed = T)], na.rm = T) :
> 'x' must be numeric"
>Then it didn't work either using as.numeric():
>> as.numeric(dat)
>Error: (list) object cannot be coerced to type 'double'
>
>Thanks!
>Dawn
>
>
>
>
>On Fri, Jul 10, 2015 at 4:35 PM, Dawn <dawn1313 at gmail.com> wrote:
>
>> Thank you all and sorry for the data messing. It has worked!
>>
>> Best,
>> Dawn
>>
>> On Fri, Jul 10, 2015 at 4:15 AM, Jim Lemon <drjimlemon at gmail.com>
>wrote:
>>
>>> Hi Dawn,
>>> Your data are a bit messed up, but try the following:
>>>
>>> colSums(dat[,grep("ABC",names(dat),fixed=TRUE)],na.rm=TRUE)
>>> colSums(dat[,grep("XYZ",names(dat),fixed=TRUE)],na.rm=TRUE)
>>>
>>> I'm assuming that you want to discard the NA values.
>>>
>>> Jim
>>>
>>> On Fri, Jul 10, 2015 at 6:52 AM, Rui Barradas <ruipbarradas at sapo.pt>
>>> wrote:
>>> > Hello,
>>> >
>>> > Please use ?dput to give a data example, like this it's completely
>>> > unreadable. If your data.frame is named 'dat' use
>>> >
>>> > dput(head(dat, 30)) # paste the outut of this in your mail
>>> >
>>> >
>>> > And don't post in html, use plain text only, like the posting
>guide
>>> says.
>>> >
>>> > Rui Barradas
>>> >
>>> >
>>> > Em 09-07-2015 18:12, Dawn escreveu:
>>> >>
>>> >> Hi,
>>> >>
>>> >> I have a big dataframe as follows
>>> >>
>>> >> 109ABC 109XYZ 18ABC 18XYZ 22XYZ 23ABC
>25ABC
>>> >> 25XYZ
>>> >> 30ABC 31XYZ 32ABC 32XYZ 34DCM 34XYZ 36ABC
>>> 36SUR
>>> >> 38DCM 38XYZ 39DCM 39SUR 41DCM 41SUR 42DCM
>42SUR
>>> >> 46SUR 52DCM 64ABC 64XYZ 65ABC 65XYZ 66ABC
>66XYZ
>>> >> 67XYZ 68ABC 68SUR 70MES 70SUR 72ABC 72XYZ
>76ABC
>>> >> 76XYZ 82ABC 85ABC POV
>>> >> Cluster_1
>17
>>> 1
>>> >> 3 10 14 5 2 2 1 1 1 2
>>> >> 2 TT:61
>>> >> Cluster_2 1 4
> 20
>>> >> 6 5 3 6 9 9 6 10 1 3 1
>>> >> 4 TT:88
>>> >> Cluster_3 3 3 6 4
> 17
>>> >> 17 18 13 17 19 22 11 5 21 8 5 18
> 4
>>> >> 7 9
>>> >> TT:227
>>> >> ........
>>> >>
>>> >> I want to get two columns, i.e, one is to sum columns for all
>>> including
>>> >> ABC for each row and the other is to sum columns for all
>including XYZ
>>> >> for
>>> >> each row.
>>> >>
>>> >> Is there some help? Thank you!
>>> >> Dawn
>>> >>
>>> >> [[alternative HTML version deleted]]
>>> >>
>>> >> ______________________________________________
>>> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>>> >> PLEASE do read the posting guide
>>> >> http://www.R-project.org/posting-guide.html
>>> >> and provide commented, minimal, self-contained, reproducible
>code.
>>> >>
>>> >
>>> > ______________________________________________
>>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> > https://stat.ethz.ch/mailman/listinfo/r-help
>>> > PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> > and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
More information about the R-help
mailing list