Generating a repeating sequence an increasingly truncated number of times in R - r

Consider some vector in R: x
x<-1:10
I'd like to create a repeating sequence of x, with the first element of each sequence truncated with each repetition, yielding the same output as would be given by issuing the following command in R:
c(1:10,2:10,3:10,4:10,5:10,6:10,7:10,8:10,9:10,10:10)
Can this be done? In reality, I'm working with a much larger vector for x. I'm playing with numerous combinations of the rep() function, to no avail.

Here's an alternative using mapply:
unlist(mapply(":", 1:10, 10))
# [1] 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 4 5 6 7
# [25] 8 9 10 4 5 6 7 8 9 10 5 6 7 8 9 10 6 7 8 9 10 7 8 9
# [49] 10 8 9 10 9 10 10

A bit of a hack, because you can decompose what you are trying to do into two sequences:
rep(0:9, 10:1) + sequence(10:1)
You can see what each part does. I don't know if there is a way to feed the parameters to rep() or seq() like you would do in a Python expansion.

unlist(sapply(1:10, function(x) { x:10 }))

Related

How can I multiply columns by columns from different matrix in R?

guys:
I have two matrix as following:
d <- cbind(c(1,2,3,4),c(1,1,1,1),c(1,2,4,8))
v <- cbind(c(2,2,2,2),c(3,3,3,3))
But I want to get a matrix consisted of divj as following:
d1v1 d1v2 d2v1 d2v2 d3v1 d3v2
2 3 2 3 2 3
4 6 2 3 4 6
6 9 2 3 8 12
8 12 2 3 16 24
This is an example of my question,I wonder if you can tell me how to write codes to solve this question.Many thanks.
matrix(apply(v,2,function(x){x*d}),4,6)

List of rows of a list

I would like to generate a list of all combinations of different elements. I found the expand.grid function which seems to be doing a great job.
lCombi<-expand.grid(c(1,2),c(4,5,6),c(7,8))
Var1 Var2 Var3
1 1 4 7
2 2 4 7
3 1 5 7
4 2 5 7
5 1 6 7
6 2 6 7
7 1 4 8
8 2 4 8
9 1 5 8
10 2 5 8
11 1 6 8
12 2 6 8
Now, if I understand well, this is a list of 3 elements of 12 rows. But what I would like is a list of 12 elements of 3. I tried to transpose it, convert it into a matrix then back to a list but none worked. So I used my pre-R way of doing things and came up with :
iLength=nrow(lCombi)
lResult<-list()
for(i in 1:iLength){
lResult[[i]]<-lCombi[i,]
}
to get my lResult list, which contains what I want. Could you please tell me the more efficient way of doing this please ?
The split function is great at chopping up a data.frame in to a list of data.frames:
lResult <- split(lCombi, 1:nrow(lCombi))
If you'd like each list element to be a vector instead, use
lapply(lResult, unlist)

R: How to use intervals as input data for histograms?

I would like to import the data into R as intervals, then I would like to count all the numbers falling within these intervals and draw a histogram from this counts.
Example:
start end freq
1 8 3
5 10 2
7 11 5
.
.
.
Result:
number freq
1 3
2 3
3 3
4 3
5 5
6 5
7 10
8 10
9 7
10 7
11 5
Some suggestions?
Thank you very much!
Assuming your data is in df, you can create a data set that has each number in the range repeated by freq. Once you have that it's trivial to use the summarizing functions in R. This is a little roundabout, but a lot easier than explicitly computing the sum of the overlaps (though that isn't that hard either).
dat <- unlist(apply(df, 1, function(x) rep(x[[1]]:x[[2]], x[[3]])))
hist(dat, breaks=0:max(df$end))
You can also do table(dat)
dat
1 2 3 4 5 6 7 8 9 10 11
3 3 3 3 5 5 10 10 7 7 5

R Create Column as Running Average of Another Column

I want to create a column in R that is simply the average of all previous values of another column. For Example:
D
X
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
I would like D$Y to be the prior average of D$X that is, D$Y is the average of all previous observations of D$X. I know how to do this using a for loop moving through every row, but is there a more efficient manner?
I have a large dataset and hardware not up to that task!
You can generate cumulative means of a vector like this:
set.seed(123)
x<-sample(20)
x
## [1] 6 15 8 16 17 1 18 12 7 20 10 5 11 9 19 13 14 4 3 2
xmeans<-cumsum(x)/1:length(x)
xmeans
## [1] 6.000000 10.500000 9.666667 11.250000 12.400000 10.500000 11.571429
## [8] 11.625000 11.111111 12.000000 11.818182 11.250000 11.230769 11.071429
## [15] 11.600000 11.687500 11.823529 11.388889 10.947368 10.500000
So D$Y<-cumsum(D$X)/1:nrow(D) should work.

R apply error - error in as.matrix.data.frame()

i am encountering a baffling error.
i am using the following function to delete rows of a dataframe containing an NA observation in any column
##### removes NA'd rows from a dataFrame
wipeNArows<-function(X){
rowsToDelete<-unique(unlist(apply(apply(X,2,is.na),2,which)))
if (length(rowsToDelete)>0){
return (X[-rowsToDelete,])
}
else{
return (X)
}
}
This function works fine normally, for instance a reproducible example is:
testFrame<-data.frame(x=rpois(20,10),y=rpois(20,10),z=rpois(20,10))
rowsToDelete<-sample(1:nrow(testFrame),5,FALSE)
testFrame$x[rowsToDelete]<-NA
testFrame
wipeNArows(testFrame) ### removes the rows where NA is encountered
Now i have a data frame containing about 2993 rows. When i pass this data frame through the function i face the following error:
Error in apply(apply(X, 2, is.na), 2, which) :
error in evaluating the argument 'X' in selecting a method for function 'apply': Error in as.matrix.data.frame(X) :
dims [product 14965] do not match the length of object [14974]
Thanks for responses,
Works fine for me, but why not use ?complete.cases
testFrame[complete.cases(testFrame),]
x y z
2 10 8 13
3 11 16 18
4 11 7 7
6 8 8 14
7 9 11 11
8 12 11 5
9 10 7 4
10 7 12 9
11 10 13 11
12 9 12 10
13 10 5 8
14 13 5 8
15 11 5 5
18 13 14 7
19 2 13 8
identical(testFrame[complete.cases(testFrame),], wipeNArows(testFrame))
[1] TRUE
Another way to solve your problem would be na.omit
na.omit(testFrame)
x y z
2 7 11 11
3 12 10 10
4 13 10 9
6 11 10 12
7 13 14 8
8 7 9 7
9 8 11 12
10 5 10 7
11 5 15 9
12 7 13 9
15 15 8 9
16 13 7 15
17 5 10 12
18 9 8 6
20 18 7 6
hmm thanks for replies,
wasn't aware of the complete.cases function. but that gives another error
Error in complete.cases(dFrame) : not all arguments have the same length
chisq.test Error Message --> appears to address this issue in a way.
the issue with the problematic data frame is that it contained a POSIXlt object column with dates. clearly complete.cases and apply internal workings aren't handling this too well. the workaround is to cast to character with strftime and then back with strptime.
thanks,
General case, if you do not have na's in your data, then as Aditya Sihag suggested, the problem could be one of your data.frame columns's datatype may be a list of objects such as a list or POSIXlt object. You can either cast them or you can just use lapply on the column alone. But again make sure your column datatype is not a list or POSIXlt before applying lapply and if yes, then just cast it.
Without the problem data, I can only suggest a different function
wipe_na_rows <- function(X){
X[!apply(X, 1, function(x) any(is.na(x))),]
}

Resources