data manupilation

Hi there, I'm a new learner in R. had a trouble to sum the data, which showed below
|bwgp|smoke|value|
|3|0|35|
|3|1|11|
|2|0|29|
|2|1|17|
|1|0|22|
|1|1|16|
|0|0|29|
|0|1|30|
this excel data is named as bwtsmk.
to obtain the sum for value by smoke status, I used the code below, but failed

smk0<-sum(bwtsmk$(value & smoke=0))

which code should I use to get it. appreciate much if someone can help.

Jason

dat <- structure(list(
  bwgp =
    c(3, 3, 2, 2, 1, 1, 0, 0),
  smoke =
    c(0, 1, 0, 1, 0, 1, 0, 1),
  value =
    c(35, 11, 29, 17, 22, 16, 29, 30)),
  class =
    c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
  row.names =
    c(NA, -8L), spec = structure(list(cols = list(bwgp = structure(list(),
  class =
    c("collector_double", "collector")), smoke = structure(list(),
  class =
    c("collector_double", "collector")), value = structure(list(),
  class =
    c("collector_double", "collector"))), default = structure(list(),
  class =
    c("collector_guess", "collector")), skip = 1),
  class = "col_spec"))  

dat
#>   bwgp smoke value
#> 1    3     0    35
#> 2    3     1    11
#> 3    2     0    29
#> 4    2     1    17
#> 5    1     0    22
#> 6    1     1    16
#> 7    0     0    29
#> 8    0     1    30
sum(dat$value)
#> [1] 189
sum(dat[3])
#> [1] 189

Created on 2020-03-31 by the reprex package (v0.3.0)

grateful to Technocrat for providing example data

library(tidyverse) # install this package to have access to the library
group_by(dat, smoke) %>% summarise(value=sum(value))
# A tibble: 2 x 2
  smoke value
  <dbl> <dbl>
1     0   115
2     1    74
1 Like

Thanks for this helpful information!
Jason

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Thanks for this, but this is not what I wanted. I've imported the excel data into R and got the same sum. what I want to get is the sum by smoke status, ie, the sum of 'value' for smoke=0 and that for smoke=1, respectively.

Hi there, here is another question related to this one, if I want to add another column as 'proportion' in the table as the result of 'value' divided by the sum by smoke status, how should I do? if possible, I would appreciate if some books or websites can be recommended on these basics.
Thanks,
Jason

sorry, I did see the code at the end.
thanks,
jason

group_by(dat, smoke) %>% 
  summarise(value=sum(value)) %>% 
  mutate(proportion=value/sum(value))

this is a good resource

1 Like