Hi,
Data which we have-
a(22,26)
b(96,55)
c(55,10)
d(66,10)
What should i have to do if i want to do a sum of all the first values e.g(22+96+55+66) and divide it to the sum of the second values e.g(26+55+10+10)?
Hi,
Data which we have-
a(22,26)
b(96,55)
c(55,10)
d(66,10)
What should i have to do if i want to do a sum of all the first values e.g(22+96+55+66) and divide it to the sum of the second values e.g(26+55+10+10)?
I'd first off be sure to familiarize yourself with the basics of R. In an intro to R course (like this one from data camp or a YouTube, or this intro videos from RStudio), you'll learn how to set-up a vector of values, and assign that to an object name, or group of vectors together into a data.frame. You'll learn syntax to refer to parts of the data.frame you've created, and how to apply functions to them.
For example below, I've created a data.frame called df
formed by two columns/variable. One called c1
and the other c2
. I then applied the sum
function to each column.
As an example:
df <- data.frame(
c1 = c(1,2,3),
c2 = c(4,5,6)
)
sum(df$c1)
#> [1] 6
sum(df$c2)
#> [1] 15
Created on 2018-04-24 by the reprex package (v0.2.0).
With a sound understanding of the basics of R (which I think you'll get after a good grounding via an intro course) stuff like this can be a snap.
This is so helpful but my concern is different sir, i want to do a sum of all the first integer values of c1 and c2 (1+4) and divide it with the sum of the second integer values of c1 and c2 (2+5)
The best way to handle this depends on what exactly you're doing. If you're just looking for a pointer to a function that will do this, check out rowSums. E.g. rowSums(df)[1] / rowSums(df)[2]
Again, I would really encourage you to work through an intro to R course to just get a base understanding how to work with data in R. You want to be able to set up a data frame, refer to different parts of it, and apply functions. Hope that's helpful!
Thank you so much sir
@EconomiCurtis gave you a great solution.
You can transform your data into a data frame or a tibble (here I will use a tibble):
library(tidyverse)
dat <- tibble(
a = c(22, 26),
b = c(96, 55),
c = c(55, 10),
d = c(66, 10)
)
Then you can use @EconomiCurtis's solution with the function rowSums
:
rowSums(dat)[1] / rowSums(dat)[2]
or you can sum across rows directly yourself:
sum(dat[1, ]) / sum(dat[2, ])
Either solution will give the result (in this example: 2.366337
).
Little side note: try to make more informative titles
you can store the variable a=c(22,26) this is the correct way to use.
for example
a <- c(22,26)
b <- c(96,55)
c <- c(55,10)
d <- c(66,10)
s <-sum(a[1:1],b[1:1],c[1:1],d[1:1])
m <-sum(a[2:2],b[2:2],c[2:2],d[2:2])
print(s)
print(m)
use this method to do wrk............enjoy 2day....
Based on how you pose your question, I would dare the comparison of asking "How can I run", without knowing how to walk.
I would therefore highly suggest, that you start with the basics, to build your R-knowledge-foundation on which you then can extend. I recommend starting here: Hands-On Programming with R - The Very Basics
by Garrett Grolemund
Happy Rrrr'ing!