From an old R thread captured in nabble the indication is that three separate operations are required to obtain the result described in the title of this post http://r.789695.n4.nabble.com/To-give-column-names-of-a-data-frame-td2249996.html:
results <- data.frame(matrix(c(1,2,3,4),nrow=2,ncol=2))
rownames(results) <- c("a","b")
colnames(results) <- c("c","d")
Can these be collapsed into a single operation?
We can use setnames and row.names to set them in one-line
setNames(data.frame(matrix(c(1,2,3,4),nrow=2,ncol=2), row.names=c("a","b")), c("c", "d"))
# c d
#a 1 3
#b 2 4
You can use the option dimnames which is part of the matrix function. The first part of dimnames are the row names, the second part the column names.
data.frame(matrix(c(1,2,3,4),nrow = 2, ncol = 2, dimnames = list(c("a","b"), c("c","d")))
The difference between matrix(c(1,2,3,4),nrow = 2, ncol = 2, dimnames = list(c("a","b"), c("c","d"))) and the previous line is that the matrix call will give you a matrix with a dimnnames attribute. The data.frame line transforms the matrix into a data.frame with row names and column headers.
Related
i have a vector
a<-as.vector(diag(5))
How to separate this vector every 5 numbers and create a data.frame by joining each in a row?
my idea is to do this
https://imgur.com/X7JLMYH
one column, each row of that column as if it were diag (5). Each line will identify a different object, so you need to follow the image order.
length must equal number of numbers within each line
We can use matrix (as the length is already a multiple of 5) and then wrap with as.data.frame
as.data.frame(matrix(a, ncol = 5, byrow = TRUE))
If we want as a single column of strings, can paste each row to create that single column data
data.frame(col1 = do.call(paste, as.data.frame(matrix(a, ncol = 5,
byrow = TRUE))))
Or place it as a list column
data.frame(col1 = I(asplit(matrix(a, ncol = 5, byrow = TRUE), 1)))
I have
d <- matrix(rnorm(6), ncol = 1,
dimnames = list(c("a", "a1", "d", "e", "f", "f2"), NULL))
I want to sort the rows in the following order: a1, a, e, d, f2, f.
Notes:
I am looking for a general solution. Of course, i know how to do it for this specific matrix.
Rownames can be all kinds of names, so any string related operation doesnt work
Matrix d will not have more than 16-20 entries. So dont worry about speed.
The matrix has always an even number of rows.
We can use a recycling logical vector to subset the row.names, alternate the names by first rbinding it to a matrix, remove the dim attibutes with c (convert to a vector) and use that as row index
d[c( rbind(row.names(d)[c(FALSE, TRUE)],
row.names(d)[c(TRUE, FALSE)])),, drop = FALSE]
# [,1]
#a1 -0.43704092
#a 0.41215035
#e 1.47443155
#d -1.78087570
#f2 -0.01673482
#f 0.98952497
Switching every pair of rows gives us row numbers 2, 1, 4, 3, 6, 5, etc. Hence, after the transformation the n-th row is the (n - (-1)^n)-th row in the original matrix.
Thus, the order of rows that you want is 1:nrow(d) - (-1)^(1:nrow(d)):
d[1:nrow(d) - (-1)^(1:nrow(d)), , drop = FALSE]
# [,1]
# a1 0.1228430
# a -1.4051684
# e -0.7928203
# d 1.3270429
# f2 0.3554126
# f -1.1388026
I am facing the following challenge:
I have a list of dataframes in R and I'd like to extract some specific information from it. Here is an example:
df_1 <- data.frame(A = c(1,2), B = c(3,4), D = c(5,6))
df_2 <- data.frame(A = c(7,8), B = c(9,10), D = c(11,12))
df_3 <- data.frame(A = c(0,1), B = c(2,3), D = c(4,5))
L <- list(df_1, df_2, df_3)
What I'd like to extract are the values at position (1,1) in each of these dataframes. In the above case this would be: 1, 7, 0.
Is there a way to extract this information easily, probably with one line of code?
As Ronak has suggested , you can use function like lapply and wrap it with unlist for desired output.
unlist(lapply(L,function(x) x[1,1]))
In addition to the *apply methods shown above, you can also do this in a Vectorized manner. Since all the data frames in your list have the same column names, and you want the first element from the first column, i.e. 'A1', then you can simply unlist (which will create a named vector) and grab the values with the name A1.
v1 <- unlist(L)
v1[names(v1) == 'A1']
#A1 A1 A1
# 1 7 0
Is there an easier (i.e. one line of code instead of two!) way to do the following:
results <- as.data.frame(str_split_fixed(c("SampleID_someusefulinfo.countsA" , "SampleID_someusefulinfo.countsB" , "SampleID_someusefulinfo.counts"), "\\.", n=2))
names(results) <- c("a", "b")
Something like:
results <- data.frame(str_split_fixed(c("SampleID_someusefulinfo.countsA" , "SampleID_someusefulinfo.countsB" , "SampleID_someusefulinfo.counts"), "\\.", n=2), colnames = c("a", "b"))
I do this a lot, and would really love to have a way to have this in one line of code.
/data.table works too, if it's easier to do there than in base data.frame/
Clarifying:
My expected output (which is achieved by running the two lines of code at the top - AND I WANT IT TO BE ONE - THAT's IT!!!) is a result data frame of the structure:
results
a b
1 SampleID_someusefulinfo countsA
2 SampleID_someusefulinfo countsB
3 SampleID_someusefulinfo counts
What I would like to do is:
CREATE the data frame from a matrix or with some content (for example the toy code of matrix(c(1,2,3,4),nrow=2,ncol=2) I provided in the first example I wrote)
SPECIFY IN THAT SAME LINE what I would like the column names of my data frame to be
Use setNames() around a data.frame
setNames(data.frame(matrix(c(1,2,3,4),nrow=2,ncol=2)), c("a","b"))
# a b
#1 1 3
#2 2 4
?setNames:
a convenience function that sets the names on an object and returns the object
> setNames
function (object = nm, nm)
{
names(object) <- nm
object
}
We can use the dimnames option in matrix as the OP was using matrix to create the data.
data.frame(matrix(1:4, 2, 2, dimnames=list(NULL, c("a", "b"))))
Or
`colnames<-`(data.frame(matrix(1:4, 2, 2)), c('a', 'b'))
How can one determine the row index-numbers corresponding to particular row names? I have a vector of row names, and I would like to use these to obtain a vector of the corresponding row indices in a matrix.
I tried row() and as.integer(rownames(matrix.object)), but neither seems to work.
In addition to which, you can look at match:
m <- matrix(1:25, ncol = 5, dimnames = list(letters[1:5], LETTERS[1:5]))
vec <- c("e", "a", "c")
match(vec, rownames(m))
# [1] 5 1 3
Try which:
which(rownames(matrix.object) %in% c("foo", "bar"))