Convert character input to vector of integers [r] [duplicate] - r

This question already has answers here:
Evaluate expression given as a string
(8 answers)
Closed 7 years ago.
How can I convert a character string such as "c(1:10)" into an actual vector of integers? My issue is that I need to read a bunch of files using read.xlsx from the package xlsx, but I need to read different rows for each file. I've got a separate file that I can read into the workspace as a data.frame (call it "MyFile") that lists in one column the names of the files and, in another column, which rows to read, but I don't know how to convert the character string into the numbers I need. I'd use regex to extract it, but that sounds like more work than I want since it's a mishmash of everything from "c(1:10)" to "c(2, 4, 6:8, 21)".
I've tried:
Files <- list()
for (i in 1:nrow(MyFiles)){
Files[[i]] <- read.xlsx(MyFiles$File[i],
sheetName = MyFiles$Tab[i],
rowIndex = MyFiles$Row[i])
}
but R doesn't know what to do with the row specification since it's currently reading it as a character string.

It's a shame there isn't a better way (as far as I know) than eval(parse(text= but that's what I use in these situations.
input <- "c(1:10)"
output <- eval(parse(text=input))
output
# [1] 1 2 3 4 5 6 7 8 9 10

Related

How can I extract the four first numbers from a variable? R [duplicate]

This question already has answers here:
Extract the first (or last) n characters of a string
(5 answers)
Closed 2 years ago.
Well, I got a variable that shows numbers like this one: 13015064000992062, and I need to extract the four first numbers and put it in another column. In this case, it will be 1301.
A friend told me that it was using stringr library, but he can't remember the code.
I am working with a data frame. So I need to get a new variable called "canton", that are the four first digits from 'Identificador'
So it looks like this:
We can use substr
permMan19$newcol <- substr(permMan19$identificador, 1, 4)

Using a character vector to refer to a data frame [duplicate]

This question already has answers here:
Access variable value where the name of variable is stored in a string
(4 answers)
Closed 2 years ago.
I want to refer to a data frame by using a character vector.
I believe the simple example below illustrates the problem.
# I have a data frame called A
A <- c(1, 2, 3, 4)
# I have a character vector called B, containing the character "A"
B <- "A"
# Now I want a third vector (C) to get the content of A, simply by referring to vector B
# Obviously, I cannot write
C <- B
# ... as this would give me
[1] "A"
# ... and NOT what I want:
[1] 1 2 3 4
How do I use a character vector to refer to the name and thus the content of an existing data frame?
PS.
I have been made aware of my questiong being a duplicate. But since the wordings are different, I didn't find the other post when searching online:
Access variable value where the name of variable is stored in a string
I keep my post, as others too might fail to find the earlier one.
It would be get to return the value of the object name as string
C <- get(B)
If there are more than objects, use mget to return the values in a list

How to get list of all combinations of pairs of character strings in R [duplicate]

This question already has answers here:
How to generate all possible combinations of vectors without caring for order?
(2 answers)
Closed 3 years ago.
I have multiple character strings, let's say 'pred_1', 'pred_2' and 'pred_3'. Now I want to get a list with all pairs of the strings. So, the resulting list should contain 'pred_1 pred_2', 'pred_1 pred_3' and 'pred_2 pred_3'. Does anyone know how to automate this for more than three character strings?
An option is combn
combn(v1, 2, simplify = FALSE)
data
v1 <- paste0("pred_", 1:3)

Edit character length of row names in R [duplicate]

This question already has answers here:
Getting and removing the first character of a string
(7 answers)
Closed 5 years ago.
I am working on Bioinformatics recently. I have to edit row.names for my variable. Here is the situation for me:
I have clinical data and gene expression values downloaded from Cancer Genome Atlas. I have to match row names but in clinical data I have row names like this "TCGA-6D-AA2E". But in gene expressions row names like "TCGA-6D-AA2E-01A-11R-A38B-07".
Normally I used "match" command to match row names but the character lengths are not same. So my question is "Is there easy way to edit character length for row names?"
You could use grep function instead:
gene.names <- c("TCGA-6D-AA2E-01A-11R-A38B-07", "TCGC-6D-AA2E-01A-11R-A38B-07", "TAGA-6D-AA2E-01A-11R-07", "TCGA-6D-AA2E-A38B-07")
pick <- "TCGA-6D-AA2E"
grep(pick, gene.names)
# [1] 1 4
Edit based on the comment: Use substr to pick 12 first characters:
substr(gene.names, 0,12)
#[1] "TCGA-6D-AA2E" "TCGC-6D-AA2E" "TAGA-6D-AA2E" "TCGA-6D-AA2E"

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