Adding successive values in a column of a csv in R [duplicate] - r

This question already has answers here:
Calculating cumulative sum for each row
(6 answers)
Closed 5 months ago.
I am new to coding so this is probably very basic. I was wondering how to add two successive values in a column of a csv along its entire length. Say this was my data:
[1] 2
[2] 3
[3] 4
[4] 5
I want to make a vector which contains 2+3, 3+4 and 4+5 (but obviously my real data set is much larger).
Thanks a lot!

I think you need cumsum():
If this is your vector:
vector <- 2:5
[1] 2 3 4 5
You would need to:
cumsum(vector)
[1] 2 5 9 14

Related

Find max values in R [duplicate]

This question already has answers here:
which.max ties method in R
(4 answers)
Closed 2 years ago.
I have the problem, now you see that:
x<-c(1,2,3,6,4,5,6)
y=(which.max(x))
print(y)
*The result is 4 because it is the position of element 6 (max value). But I want the result returned is 4 and 7.
How can I do that?
Try the below:
which(x==max(x))
[1] 4 7
If you have potential NA values, use
which(x==max(x, na.rm=T))

Create a vector: c(1,2,4,2,2) and use rank variable to return c(1,2,5,2,2) [duplicate]

This question already has answers here:
Extending rank() "Olympic Style"
(2 answers)
R - Add row index to a data frame but handle ties with minimum rank
(3 answers)
Rank with Ties in R
(1 answer)
Closed 2 years ago.
I would like to create a vector:
c(1,2,4,2,2)
And use rank variable to return:
c(1,2,5,2,2)
How do I use rank?
Check out the ?rank help page for options, specifically the ties.method= paramter. Specifically the output you want can be generated with ties.method="min"
rank(c(1,2,4,2,2), ties.method = "min")
# [1] 1 2 5 2 2
Using min_rank from dplyr
library(dplyr)
min_rank(c(1,2,4,2,2))
#[1] 1 2 5 2 2

vector of historical highs of another vector [duplicate]

This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 3 years ago.
I searched at the forums but I could not find an answer for this.
I am looking for a way to convert
c(1,3,4,2,7,12,6,8,15)
to
c(1,3,4,4,7,12,12,12,15) using no loops.
It can be defined as a vector of historical highs of another vector.
This is an example, my data length will be fairly long.
thanks a lot,
Emre
We can use cummax from base R
cummax(v1)
#[1] 1 3 4 4 7 12 12 12 15
data
v1 <- c(1,3,4,2,7,12,6,8,15)

R: Extracting non-duplicated values from vector (not keeping one value for duplicates) [duplicate]

This question already has answers here:
Finding ALL duplicate rows, including "elements with smaller subscripts"
(9 answers)
How can I remove all duplicates so that NONE are left in a data frame?
(3 answers)
Closed 5 years ago.
I would like to keep the non-duplicated values from a vector, but without retaining one element from duplicated values. unique() does not work for this. Neither would duplicated().
For example:
> test <- c(1,1,2,3,4,4,4,5,6,6,7,8,9,9)
> unique(test)
[1] 1 2 3 4 5 6 7 8 9
Whereas I would like the result to be: 2,3,5,7,8
Any ideas on how to approach this? Thank you!
We can use duplicated
test[!(duplicated(test)|duplicated(test, fromLast=TRUE))]
#[1] 2 3 5 7 8
You can use ave to count the length of sub-groups divided by unique values in test and retain only the ones whose length is 1 (the ones that have no duplicates)
test[ave(test, test, FUN = length) == 1]
#[1] 2 3 5 7 8
If test is comprised of characters, use seq_along as first argument of ave
test[ave(seq_along(test), test, FUN = length) == 1]

Read a file without delimination to R as a matrix [duplicate]

This question already has answers here:
How can I read a matrix from a txt file in R?
(2 answers)
Closed 7 years ago.
I have a very big file like this (no separator between characters):
1234
3456
2345
I want to read it to R as a matrix and get this:
1 2 3 4
3 4 5 6
2 3 4 5
This question is like this question: read in matrix into r without delimination but I am looking for a better way. I do not want to put the number of columns - I want the number of columns to be a variable in the code and support big files.
How about:
library(readr)
my_file <- "big_file.txt"
my_matrix <- as.matrix(read_fwf(my_file, fwf_widths(rep(1,nchar(readLines(my_file, n=1))))))
nchar(readLines(my_file, n=1)) reads the first line and counts the number of characters. This is the multiplier of for the rep() for specifying the fwf_widths.
This assumption being that all your numbers are integers between 0 and 9.

Resources