简体   繁体   中英

how do i subset many variable in R?

i want to ask how do i subset many data from 5k variable.

i want to subset all the reciept ID, but the ID is too many (about 24,5 k). i want ro find the total price of the reciept. How do i subset those ID without manual typing?

then i want to convert it to data frame.

i also use dplyr<\/code> package in R

test = data %>%
  group_by(InvoiceNo)%>%
  dplyr::summarise(n= n())

bon = test$InvoiceNo

result = for (i in bon){
  data = subset(data, subset = InvoiceNo == i)
  print(sum(data$Price_Total))
}

test =  data.frame("test" = bon,
                   "wiw" = result)

You're on the right track with group_by(InvoiceNo) %>% summarize()<\/code> . Since you want to sum up price within each InvoiceNo, you would just put that within the call to summarize()<\/code> :

test = data %>%
  group_by(InvoiceNo)%>%
  summarise(Price_Total = sum(Price_Total))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM