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

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

Related

Finding the most repeated value using table() function [duplicate]

This question already has answers here:
How to retrieve the most repeated value in a column present in a data frame
(9 answers)
Closed 2 years ago.
I was given a sample vector v and was asked to use R code to extract, as a number (meaning: not as a character string), the value that was repeated the most times in v.
(Hints: use table(); note that which.max() gives you index of a vector's maximum value, like the maximum value within a table; names() allows you the extract the values of the original vector, when applied to the output of table().)
My answer is as follows:
names(which.max(table(v)))
it returns the correct answer as a string, not as a number. Am i using the hint correctly? Thanks.
names return the number as character, perhaps add as.integer/as.numeric to convert it to number.
as.integer(names(which.max(table(v))))
Moreover, in case of tie which.max would return only the first maximum. If you want all the values which are tied you can use :
v <- c(1, 1, 2, 4, 5, 3, 3)
as.integer(names(which.max(table(v))))
#[1] 1
tab <- table(v)
as.integer(names(tab[max(tab) == tab]))
#[1] 1 3

Viewing single column of data frame in R [duplicate]

This question already has answers here:
How to subset matrix to one column, maintain matrix data type, maintain row/column names?
(1 answer)
How do I extract a single column from a data.frame as a data.frame?
(3 answers)
Closed 5 years ago.
I am running a simulation model that creates a large data frame as its output, with each column corresponding to the time-series of a particular variable:
data5<-as.data.frame(simulation3$baseline)
Occasionally I want to look at subsets, especially particular columns, of this data frame in order to get an idea of the output. For this I am using the View-function like so
View(data5[1:100,1])
for instance, if I wish to see the first 100 rows of column 1. Alternatively, I also sometimes do something like this, using the names of the time series:
timeframe=1:100
toAnalyse=c("u","u_n","u_e","u_nw")
View(data5[timeframe,toAnalyse])
In either case, there is an annoying display problem when I am trying to view a single column on its own (as for instance with View(data5[1:100,1])), whereby what I get looks like this:
Example 1
As you can see, the top of the table which would usually contain the name of the variable in the dataset instead contains a string of all values that the variable takes. This problem does not appear if I select 2 or more columns:
Example 2
Does anyone know how to get rid of this issue? Is there some argument that I can feed to View to make sure that it behaves nicely when I ask it to just show a single column?
View(data5[1:100,1, drop=FALSE])
When you access a single column of a data frame it is converted to a vector, drop=FALSE prevents that and retains the column name.
For instance:
> df
n s b
1 2 aa TRUE
2 3 bb TRUE
3 5 cc TRUE
> df[, 1]
[1] 2 3 5
> df[, 1, drop=FALSE]
n
1 2
2 3
3 5

Why names(a[2])<- "e" not working, but names(a)[2]<-"e" is working in R Language [duplicate]

This question already has an answer here:
Naming list elements in R
(1 answer)
Closed 5 years ago.
i tried Naming the Vector in R.
a<-1:5
names(a[2])<-"e" #this is not showing any warning or error but Naming is not done
but
names(a)[2]<-"e" # this is is Naming the Element properly.
Kindly help with Explanation.
The basic difference is in understanding what a[1] and names(a)[1] stands for.
a<-1:5
(This assigns values 1 to 5 and creates a vector)
a[1] # This gives below output i.e the value stored at first location
[1] 1
names(a)[1] # Shows the label associated with the value in this case 'NULL' yet
NULL
Now assigning the required name to the value
names(a)[2]<-"e"
This does the correct assignment and is how the R expects the code. You can then extract the value by element name namely
a["e"] #will give output
2

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"

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

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

Resources