Why Can't I Access Column Name After Cbind() In R - r

I have combined different matrix with same row size using cbind(). I observe that the column name of resulting matrix is not accessible using $.
Is there any specific reason for this? Thanks.

The $ operator does not work for matrices. Instead use matrix_name[,'column_name'] or matrix_name[,i], being i the column index.

Related

Adding header's name for dataset in kbl

When I use kbl in r markdown, sometimes the header's name lost. Look the pic, the head column's names are all "x" instead of its original one (df3:"No."variable","p-value"; df.gini.sort:"variable","gini score. Could anyone help to figure out how to fix that? Thanks~~!
We can use cbind instead of c as c will concatenate the data.frame to a named list of vectors (data.frame - is a list of vectors/columns of equal length with additional attribute). Here, we assume both the datasets to have the same number of rows
library(kableExtra)
kbl(cbind(df3, df.gini.sort))
If we are using c, then wrap with data.frame afterwards
kbl(data.frame(c(df3, df.gini.sort)))

Role of square brackets

I got this code from elsewhere and I wondering if someone can explain what the square brackets are doing.
matrix1[i,] <- df[[1]][]
I am using this to assign values to a matrix and it works but I am not sure what exactly it's doing. What does the initial set of [[]] mean followed by another []?
This might help you understand a bit. You can copy and paste this code and see the differences between different ways of indexing using [] and $. The only thing I can't answer for you is the second empty set of square brackets, from my understanding that does nothing, unless a value is within those brackets.
#Retreives the first column as a data frame
mtcars[1]
#Retrieves the first column values only (three different methods of doing the same thing)
mtcars[,1]
mtcars[[1]]
mtcars$mpg
#Retrieves the first row as a data frame
mtcars[1,]
#I can use a second set of brackets to get the 4th value within the first column
mtcars[[1]][4]
mtcars$mpg[4]
The general function of [ is that of subsetting, which is well documented both in help (as suggested in comments), and in this piece. The rest of of my answer is heavily based on that source.
In fact, there are operators for subsetting in R; [[,[, and $.
The [ and $ are useful for returning the index and named position, respectfully, for example the first three elements of vector a = 1:10 may be subsetted with a[c(1,2,3)]. You can also negatively subset to remove elements, as a[-1] will remove the first index.
The $ operator is different in that it only takes element names as input, e.g. if your df was a dataframe with a column values, df$values would subset that column. You can achieve the same [, but only with a quoted name such as df["values"].
To answer more specifically, what does df[[1]][] do?
First, the [[-operator will return the 1st element from df, and the following empty [-operator will pull everything from that output.

R: How do I filter a named list on names present in a vector?

I am trying to obtain a subset of a named list, based on element present in another list.
nammedlist<-list( "a"=c(1,2,3,4), "b"=c(2,4,5), "c"=c(9,5,3,2))
selection<-c("a","c")
desired output:
namedlist2<-list( "a"=c(1,2,3,4), "c"=c(9,5,3,2))
I am considering writing a for loop checking for each name if it is present and then extracting it. But their must be a cleaner way to do this.
You can use the names as an index:
nammedlist[selection]
will give you what you want.
Note that you use single brackets, not double brackets. Single brackets mean that you want a subset of the list. Double brackets mean you want to extract an element from the list.

Logical comparison of elements from named list vs named vector in R

Sorry if this is a duplicate, I read through a number of threads but couldn't really find a good explanation.
I have a dataset (dataframe) where I calculated the mean value of each column. I now want to do some logical comparisons between these values. I used lapply to get the means
means_list <- lapply(dataset_df, mean)
which outputs a named list. But when I try to compare two elements of this list, e.g.
means_list["condition1"] > means_list["condition2"]
I get an error ("comparison of these types is not implemented").
I don't get that error if I use sapply instead so that I'm working with a named vector. I can also get around the error by converting the list to a dataframe with as.data.frame first.
So, I feel like I'm doing something wrong when subsetting a named list here but I don't quite understand how. Is there a correct way to subset the list so that I can do the logical comparison? Or is this not possible with named lists?
Thanks!
To access to the element of a list by its name, you have to use double brackets:
means_list[["condition1"]] > means_list[["condition2"]]

How to calculate combination of Data frame in R

I am a beginner in R program.
I imported a csv file. This file only contains one column with 50 characters, but R classifies it as a dataframe. I need all possible combinations within elements of this column. I think I need to work with a vector not with a data frame, how can I do it?
Thank you!
Actually your data frame already contains the vector you need. You can call it with
dataframe$column_name
The text before the $ operator specifies your data frame, and after is your vector, which is a column in your data frame. So when you run your calculations you can just write
function(dataframe$column_name)
In your specific case with a single vector, it may be simplest to change the dataframe into a 2d vector. But when you start manipulating your data, you'll likely store more vectors of variables. You'll want to keep those vectors organized within data frames.
Do you mean unlist?
You can use it to change a data frame into a vector, then you can use combn to get combination.

Resources