Creating Vectors sequence in R [duplicate] - r

This question already has answers here:
Alternate, interweave or interlace two vectors
(2 answers)
Closed 1 year ago.
I want to write a R program that creates the vector 0.1^3, 0.2^1, 0.1^6, 0.2^4, ..., 0.1^36, 0.2^34.
v=c(seq(3,36,3))
w=c(seq(1,34,3))
x=c(0.1^v)
y=c(0.2^w)
z=c(x,y)
Please help.

rbind to a matrix and convert to vector again:
c(rbind(x, y))
Or more directly:
rep(c(0.1, 0.2), 12)^c(rbind(seq(3,36,3), seq(1,34,3)))

You can use matrix to create the desired vector.
c(matrix(z, 2, byrow=TRUE))
# [1] 1.000000e-03 2.000000e-01 1.000000e-06 1.600000e-03 1.000000e-09
# [6] 1.280000e-05 1.000000e-12 1.024000e-07 1.000000e-15 8.192000e-10
#[11] 1.000000e-18 6.553600e-12 1.000000e-21 5.242880e-14 1.000000e-24
#[16] 4.194304e-16 1.000000e-27 3.355443e-18 1.000000e-30 2.684355e-20
#[21] 1.000000e-33 2.147484e-22 1.000000e-36 1.717987e-24

Related

which() and match() functions can't find matches in my numeric vector [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 1 year ago.
I have a numeric vector of 254 elements called smoothed_auto:
smoothed_auto
[1] 0.4167147 0.4168202 0.4169259 0.4170314 0.4171361 0.4172395 0.4173410 0.4174401 0.4175435 0.4176561 0.4177743
[12] 0.4178945 0.4180133 0.4181271 0.4182322 0.4183252 0.4184301 0.4185647 0.4187166 0.4188734 0.4190225 0.4191516
[23] 0.4192480 0.4192995 0.4193022 0.4192707 0.4192201 0.4191651 0.4191207 0.4191017 0.4191231 0.4191997 0.4192655
(...)
And I am trying to find the indices of specific elements with the which() and match() functions:
which(smoothed_auto == 0.4167147)
#integer(0)
match(c(0.4167147), smoothed_auto)
#[1] NA
I have tried this with multiple elements and always get the same result. Any help?
This is R FAQ 7.31 and a duplicate of Why are these numbers not equal?.
A way of solving it is with a function I posted here.
are.equal <- function(x, y, tol = .Machine$double.eps^0.5) abs(x - y) < tol
which(are.equal(smoothed_auto, 0.4167147))
#[1] 1
Data
smoothed_auto <- scan(text = "
0.4167147 0.4168202 0.4169259 0.4170314 0.4171361 0.4172395 0.4173410 0.4174401 0.4175435 0.4176561 0.4177743
0.4178945 0.4180133 0.4181271 0.4182322 0.4183252 0.4184301 0.4185647 0.4187166 0.4188734 0.4190225 0.4191516
0.4192480 0.4192995 0.4193022 0.4192707 0.4192201 0.4191651 0.4191207 0.4191017 0.4191231 0.4191997 0.4192655
")
Remove the c in match(c(0.4167147), smoothed_auto)
smoothed_auto <- as.vector(c(0.4167147, 0.4168202, 0.4169259, 0.4170314, 0.4171361, 0.4172395,
0.4173410, 0.4174401, 0.4175435, 0.4176561, 0.4177743, 0.4178945,
0.4180133, 0.4181271, 0.4182322, 0.4183252, 0.4184301, 0.4185647,
0.4187166, 0.4188734, 0.4190225, 0.4191516, 0.4192480, 0.4192995,
0.4193022, 0.4192707, 0.4192201, 0.4191651, 0.4191207, 0.4191017,
0.4191231, 0.4191997, 0.4192655))
which(smoothed_auto == 0.4167147)
match(0.4167147, smoothed_auto)
Output:
> smoothed_auto <- as.vector(c(0.4167147, 0.4168202, 0.4169259, 0.4170314, 0.4171361, 0.4172395,
+ 0.4173410, 0.4174401, 0.4175435, 0.4176561, 0.4177743, 0.4178945,
+ 0.4180133, 0.4181271, 0.4182322, 0.4183252, 0.4184301, 0.4185647,
+ 0.4187166, 0.4188734, 0.4190225, 0.4191516, 0.4192480, 0.4192995,
+ 0.4193022, 0.4192707, 0.4192201, 0.4191651, 0.4191207, 0.4191017,
+ 0.4191231, 0.4191997, 0.4192655))
> which(smoothed_auto == 0.4167147)
[1] 1
> match(0.4167147, smoothed_auto)
[1] 1

Element wise concatenation of nested list [duplicate]

This question already has answers here:
Paste multiple columns together
(11 answers)
Closed 4 years ago.
I have a nested list
l1 <- letters
l2 <- 1:26
l3 <- LETTERS
list <- list(l1,l2,l3)
Is there an elegant way to concatenate all the elements in inner vectors to form one character vector (possibly using paste), the assumption is that all the inner vectors are of the same length.
I would like my final result to be
[1] "a1A"
[2] "b2B"
[3] "c3C"
[4] "d4D"
....
[26] "z26Z"
Try:
apply(sapply(list,paste0),1,paste0,collapse="")
[1] "a1A" "b2B" "c3C" "d4D" "e5E" "f6F" "g7G" "h8H" "i9I" "j10J" "k11K" "l12L" "m13M" "n14N" "o15O" [16] "p16P" "q17Q" "r18R" "s19S" "t20T" "u21U" "v22V" "w23W" "x24X" "y25Y" "z26Z"
user20650's solution is probably as elegant as you are going to get. But for what it's worth, here's a quick hack in dplyr:
library(dplyr)
ll <- list(l1,l2,l3) # I try not to use "list" as a name. Gets confusing sometimes.
as.data.frame(ll) %>%
mutate(x = paste0(.[[1]], .[[2]], .[[3]])) %>%
.$x
# returns
[1] "a1A" "b2B" "c3C" "d4D" "e5E" "f6F" "g7G" "h8H" "i9I" "j10J" "k11K" "l12L"
[13] "m13M" "n14N" "o15O" "p16P" "q17Q" "r18R" "s19S" "t20T" "u21U" "v22V" "w23W" "x24X"
[25] "y25Y" "z26Z"

How keep only date variable without time-field string [duplicate]

This question already has answers here:
Date conversion from POSIXct to Date in R
(3 answers)
Closed 5 years ago.
My question takes a general aspect comparing to which was proposed here How to remove time-field string from a date-as-character variable?.
In fact, suppose I have this date type variable:
> head(DataDia$Date)
[1] "2016-09-13 15:56:30.827" "2016-12-12 13:39:17.537" "2016-09-16 21:57:24.977" "2016-09-23 11:19:22.010"
[5] "2017-01-11 20:06:58.490" "2016-10-21 23:40:43.927"
How do I delete all time-field strings and just keep the date format. SO that I get this:
> head(DataDia$Date)
[1] "2016-09-13" "2016-12-12" "2016-09-16" "2016-09-23"
[5] "2017-01-11" "2016-10-21"
Note please that I am working on a data table. So I need a way using data.table
operations.
Just use as.Date(DataDia$Date).
You Can use:
as.POSIXct(Df$Date,format='%Y-%m-%d',tz= "UTC")
Combining as.Date and as.character
x = c("2016-09-13 15:56:30.827", "2016-12-12 13:39:17.537", "2016-09-16 21:57:24.977", "2016-09-23 11:19:22.010",
"2017-01-11 20:06:58.490", "2016-10-21 23:40:43.927")
y = as.character(as.Date(x, format = "%Y-%m-%d"))
y
[1] "2016-09-13" "2016-12-12" "2016-09-16" "2016-09-23" "2017-01-11" "2016-10-21"

Concatenating two vectors in R [duplicate]

This question already has answers here:
How to concatenate factors, without them being converted to integer level?
(9 answers)
Closed 5 years ago.
I want to concatenate two vectors one after the other in R. I have written the following code to do it:
> a = head(tracks_listened_train)
> b = head(tracks_listened_test)
> a
[1] cc1a46ee0446538ecf6b65db01c30cd8 19acf9a5cbed34743ce0ee42ef3cae3e
[3] 9e7fdbf2045c9f814f6c0bed5da9bed7 3441b1031267fbb6009221bf47f9c5e8
[5] 206c8b79bd02beeea200879afc414879 1a7a95e3845a6815060628e847d14362
18585 Levels: 0001a423baf29add84af6ec58aeb5b90 ...
> b
[1] 7251a7694b79aa9a39f9a1a5f5c8a253 2f362377ef0e7bca112233fdda22a79c
[3] c1196625b1b733b62c43935334e1d190 58e41e462af4185b08231a41453c3faf
[5] 1cc2517fa9c037e02a14ce0950a28f67
10186 Levels: 0001a423baf29add84af6ec58aeb5b90 ...
> res = c(a,b)
> res
[1] 14898 1898 11556 3859 2408 1950 4473 1865 7674 3488 1130
However, I get the unexpected result of the resultant vector. What could the problem be?
We need to convert the factor class to character class
c(as.character(a), as.character(b))
The reason we get numbers instead of the character is based on the storage mode of factor i.e. an integer. So when we do the concatenation, it coerces to the integer mode

making a named list from vectors without copying/pasting

I have several vectors to combine into a named list ("my_list"). The names of the vectors are already stored in the vector ("zI").
> zI
[1] "Chemokines" "Cell_Cycle" "Regulation"
[4] "Senescence" "B_cell_Functions" "T_Cell_Functions"
[7] "Cell_Functions" "Adhesion" "Transporter_Functions"
[10] "Complement" "Pathogen_Defense" "Cytokines"
[13] "Antigen_Processing" "Leukocyte_Functions" "TNF_Superfamily"
[16] "Macrophage_Functions" "Microglial_Functions" "Interleukins"
[19] "Cytotoxicity" "NK_Cell_Functions" "TLR"
If it's a small number of vectors, I'd simply do
my_list <- setNames(list(Chemokines, Adhesion), c("Chemokines", "Adhesion"))
I'd like to find a smarter way, other than to combine the vector names into a long string and then copying/pasting.
> toString(zI)
[1] "Chemokines, Cell_Cycle, Regulation, Senescence, B_cell_Functions, T_Cell_Functions, Cell_Functions, Adhesion, Transporter_Functions, Complement, Pathogen_Defense, Cytokines, Antigen_Processing, Leukocyte_Functions, TNF_Superfamily, Macrophage_Functions, Microglial_Functions, Interleukins, Cytotoxicity, NK_Cell_Functions, TLR"
> my_lists <- list(Chemokines, Cell_Cycle, Regulation, Senescence, B_cell_Functions, T_Cell_Functions, Cell_Functions, Adhesion, Transporter_Functions, Complement, Pathogen_Defense, Cytokines, Antigen_Processing, Leukocyte_Functions, TNF_Superfamily, Macrophage_Functions, Microglial_Functions, Interleukins, Cytotoxicity, NK_Cell_Functions, TLR)
> my_lists <- setNames(my_lists, zI)
This is probably a really fundamental question, but I've searched and read about 10 separate threads and still can't figure it out. Much thanks for any help!
We can use mget to get the values of the character strings.
mget(zI)

Resources