This question already has answers here:
Sum values in a rolling/sliding window
(6 answers)
Running Sum in R data.table [duplicate]
(1 answer)
Closed 4 years ago.
I have a little problem...let's say I have a data.table with one numerical column like:
NR
1
2
3
5
7
10
1
I want to create a new column which is computed in this way:
in row j I want the sum of NR in the rows j, j+1, j+2. So I want this result:
NR NEW_NR
1 6
2 10
3 15
5 22
7 18
10 11
1 1
Could anyone help me pls?
Related
This question already has answers here:
Sort (order) data frame rows by multiple columns
(19 answers)
Closed 1 year ago.
I'm beginning with R and I have a question.
I have this:
x <- data.frame(x0=c(1:10), x1=c("z", "a","a","a","a","a","c","b","b","b"))
So basically two columns. I want to sort alphabetically taking the entire row of the data frame.
So that 1 - z (both x0 and x1) appear at the end.
I've tried sort() but just managed to sort the column x1 and not both x0 and x1.
Thanks
In base R you can subset and order:
x[order(x$x1),]
x0 x1
2 2 a
3 3 a
4 4 a
5 5 a
6 6 a
8 8 b
9 9 b
10 10 b
7 7 c
1 1 z
With dplyr you use arrange:
library(dplyr)
x %>%
arrange(x1)
This question already has answers here:
Unique combination of all elements from two (or more) vectors
(6 answers)
Generate list of all possible combinations of elements of vector
(10 answers)
Closed 2 years ago.
I have a df with one column and I would like to make combinations with the values of this column in order to have a new df with two columns, like he simple example below: (Obs: my df has ~5000 rows)
df
CG
1
2
3
##I would like a result similar to this:
> head(df1)
C1 C2
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Does someone could help me?
Thank you in advance
This question already has answers here:
Increment by 1 for every change in column
(6 answers)
Closed 2 years ago.
v <- c(1,1,2,3,3,3,1,1,3,4,4)
I'm trying to create a vector of elements in which the first occurrence of a non-repeated number always increases by one relative to the previous number.
This is the desired output
1,1,2,3,3,3,4,4,5,6,6
What would an efficient way of doing this would be?
A base R option with rle
> with(rle(v),rep(seq_along(values),lengths))
[1] 1 1 2 3 3 3 4 4 5 6 6
or data.table::rleid
> data.table::rleidv(v)
[1] 1 1 2 3 3 3 4 4 5 6 6
This question already has answers here:
Filter data.frame rows by a logical condition
(9 answers)
Closed 4 years ago.
An example of the data frame I have is:
Index TimeDifference
1
2
3 20
4
5 67
I want to delete all rows that are blank (these are blank and NOT na). Hence the following data frame I want is:
Index TimeDifference
3 20
5 67
Thanks
Assuming that TimeDifference is a character column:
df <- data.frame(Index=1:5, TimeDifference=c("","","20","","67"))
Then you can use:
df[-which(df$TimeDifference==""),]
or
df[!(df$TimeDifference==""),]
or
df[df$TimeDifference!="",]
which gives:
Index TimeDifference
3 3 20
5 5 67
df <- df[as.character(df$TimeDifference)!= "" ,]
This question already has answers here:
How to extract the row with min or max values?
(4 answers)
Closed 5 years ago.
In language R. Working with Data Frames.
The data frame is for keeping track of game scores for basketball. The rows represent different basketball teams. One of the columns represent overall score for the season.
How would I select the row (the team) that has highest score?
You can use which.max():
set.seed(1)
df = data.frame(team=letters[1:10],score=sample(1:10,10))
df[which.max(df$score),]
df:
team score
1 a 3
2 b 4
3 c 5
4 d 7
5 e 2
6 f 8
7 g 9
8 h 6
9 i 10
10 j 1
Output:
team score
9 i 10