Some timings of the different suggestions below
> library(microbenchmark)
> x <- matrix(1:20, nrow=10)
> s <- c(1,2)
> option1 <- sapply(1:2, function(i) x[,i]/s[i])
> option2 <- x %*% diag(1/s)
> option3 <- sweep(x, 2, s, '/')
> all.equal(option1,option2)
[1] TRUE
> all.equal(option2,option3)
[1]
Why not use standard matrix multiplication which is straightforward here:
x %*% diag(1/s)
HTH,
Eric
On Wed, Mar 3, 2021 at 7:13 AM Harold Doran <
harold.do...@cambiumassessment.com> wrote:
> To make sure the scalar is used instead of using the recycled vector s,
> maybe like this
>
> x <- matr
Hello,
Your col_types argument is wrong, you only have col_date.
library(tidyverse)
urlfile <-
"https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv";
#GN added 3/3
cols_spec <- cols(
date = col_date(format = ""),
county = col_character(),
state = col_characte
Hi Team
I have a tibble like the below :
class(us_counties)
[1] "tbl_df" "tbl""data.frame"
head(us_counties)
# A tibble: 6 x 8
date deaths Todays_deaths county state fips
1 2020-03-19 0 0 Abbev~ Sout~ 45001
2 2020-03-20 0
To make sure the scalar is used instead of using the recycled vector s, maybe
like this
x <- matrix(1:20, nrow=10)
s <- c(1,2)
sapply(1:2, function(i) x[,i]/s[i])
-Original Message-
From: R-help On Behalf Of Steven Yen
Sent: Wednesday, March 3, 2021 6:00 AM
To: R-help Mailing List
Sub
Thanks to all. sweep is convenient.
On 2021/3/3 下午 07:16, Rui Barradas wrote:
Hello,
I forgot about sweep:
sweep(x, 2, s, '/')
sweep(x, 2, 1:4, '/')
Hope this helps,
Rui Barradas
Às 11:12 de 03/03/21, Rui Barradas escreveu:
Hello,
Maybe define an infix operator?
`%!%` <- function(x, y
Hello,
I forgot about sweep:
sweep(x, 2, s, '/')
sweep(x, 2, 1:4, '/')
Hope this helps,
Rui Barradas
Às 11:12 de 03/03/21, Rui Barradas escreveu:
Hello,
Maybe define an infix operator?
`%!%` <- function(x, y) {
stopifnot(ncol(x) == length(y))
t(t(x)/y)
}
x <- matrix(1:20, ncol =
Hello,
Maybe define an infix operator?
`%!%` <- function(x, y) {
stopifnot(ncol(x) == length(y))
t(t(x)/y)
}
x <- matrix(1:20, ncol = 2)
s <- 1:2
x %!% s
x %!% 1:4
Hope this helps,
Rui Barradas
Às 11:00 de 03/03/21, Steven Yen escreveu:
I have a 10 x 2 matrix x. Like to divide the fir
I have a 10 x 2 matrix x. Like to divide the first column by s[1] and
second column by s[2]. The following lines work but are clumsy. Any
idea? Thanks.
> x
[,1] [,2]
[1,] 1 11
[2,] 2 12
[3,] 3 13
[4,] 4 14
[5,] 5 15
[6,] 6 16
[7,] 7 17
[8,] 8
9 matches
Mail list logo