I have a data frame df in the following form:
v2 v3
2.3 c(1,5,8,2)
1.2 c(2,4,3,2)
The typeof(df$v3[1]) is list, and I want to convert it to vector. So I write a `sapply`` function and run it:
df$v3 <- sapply(
df$v3,
function(x) {x <- unlist(x)}
)
But it just keeps running and does not generate any result. I also tried lapply but it can't give me the expected result. Instead, it again generates a list.
Any my dput(droplevels(head(df))) result is :
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0))), .Names = c("V2", "V3"), row.names = c(NA,
-6L), .internal.selfref = <pointer: 0x102010578>, class = c("data.table", "data.frame"))
Could you please tell me how to solve it?
EDIT:
I run the df$V3 <- unlist(df$V3) for a long time. But the type is still list and a warning is generated:
Warning messages:
1: In `[<-.data.table`(x, j = name, value = value) :
Supplied 496450000 items to be assigned to 99290 items of column 'V3' (496350710 unused)
2: In `[<-.data.table`(x, j = name, value = value) :
Coerced 'double' RHS to 'list' to match the column's type; may have truncated precision. Either change the target column to 'double' first (by creating a new 'double' vector length 99290 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'list' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.
Related
This question already has answers here:
Convert the values in a column into row names in an existing data frame
(5 answers)
Closed 27 days ago.
I want to assign the first column as rownames of kirp.mut.
rownames(kirp.mut) <- kirp.mut[,1]
kirp.mut[,1] <- NULL
Traceback:
> rownames(kirp.mut) <- kirp.mut[,1]
Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length
In addition: Warning message:
Setting row names on a tibble is deprecated.
Dimensions:
> dim(kirp.mut)
[1] 283 8654
Class:
> class(kirp.mut)
[1] "tbl_df" "tbl" "data.frame"
typeof(kirp.mut)
[1] "list"
Data:
> dput(kirp.mut[1:10,1:10])
structure(list(sample_id = c("TCGA-2Z-A9J1-01A-11D-A382-10",
"TCGA-B9-A5W9-01A-11D-A28G-10", "TCGA-GL-A59R-01A-11D-A26P-10",
"TCGA-2Z-A9JM-01A-12D-A42J-10", "TCGA-A4-A57E-01A-11D-A26P-10",
"TCGA-BQ-7044-01A-11D-1961-08", "TCGA-HE-7130-01A-11D-1961-08",
"TCGA-UZ-A9Q0-01A-12D-A42J-10", "TCGA-HE-A5NI-01A-11D-A26P-10",
"TCGA-WN-A9G9-01A-12D-A36X-10"), NBPF1 = c(1, 0, 0, 0, 0, 0,
0, 0, 0, 0), CROCC = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), SF3A3 = c(1,
0, 0, 0, 0, 0, 0, 0, 0, 0), GUCA2A = c(1, 0, 0, 0, 0, 0, 0, 0,
0, 0), RAVER2 = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), ACADM = c(1,
0, 0, 0, 0, 0, 0, 0, 0, 0), PDE4DIP = c(1, 0, 0, 0, 0, 0, 0,
0, 0, 0), NUP210L = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), NCF2 = c(1,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
A tibble cannot have row names assigned. You could convert it to another format, such as a data frame, then assign row names. You can also do this tidyverse solution using column_to_rownames on your tibble without explicitly converting to another form, but it will do so internally and return a data.frame:
library(tidyverse)
library(dplyr)
kirp.mut <- kirp.mut %>%
column_to_rownames(var = "sample_id")
See the technical documentation here on row names and tibbles
Convert to matrix, excluding 1st column, then assign rownames:
m <- as.matrix(kirp.mut[, -1])
rownames(m) <- kirp.mut$sample_id
Or to a dataframe
#convert tibble to data.frame, then add rownames
df <- as.data.frame(kirp.mut[, -1])
rownames(df) <- kirp.mut$sample_id
I would like to replace characters for specifics numeric vector.
I have this df:
First Second Third
A C D
F R K
and I also have vectors like these
A = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
R = c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
N = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
I have tried several times but I can't do it. Does anyone have some advice or idea?
An option would be to unlist (convert to character if it is factor) and then use mget to return the values for that object in a list
lst1 <- mget(as.character(unlist(df)))
I am trying to calculate robustness, a graph theory measure using R (braingraph package).
Robustness = robustness(my_networkgraph, type = c("vertex"), measure = ("btwn.cent"))
I get the following error, when I use the above robustness function:
Error in order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector
Any idea, what I am doing wrong here?
My network, which is a matrix has been converted to igraph object and robustness was calculated.
My network as a matrix:
mynetwork <- matrix(c(0, 1, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0), nrow = 8)
This matrix was converted as igraph using the following code:
my_networkgraph <-graph_from_adjacency_matrix(mynetwork, mode = c("undirected"),weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)
Please help me to understand the above error
Thanks
Priya
There was a bug in the above function. To run the robustness code, you will need to supply a vertex attribute to your network: V(network)$degree <- degree(network) V(network)$btwn.cent <- centr_betw(network)$res
I am trying to compute the matrix exponential of a matrix M, such that M = 1i*(pi/2)*Spin_Sx, where Spin_Sx is a matrix with real values. I get the following error:
Matrix::expm(M)
Error in expm(Matrix(x)) :
error in evaluating the argument 'x' in selecting a method for function'expm': Error in all0(object[lower.tri(object)]) :
Argument must be numeric-like atomic vector
Spin_Sx <- structure(c(0, 1.22474487139159, 0, 0, 0, 0, 0, 1.22474487139159,
0, 1.58113883008419, 0, 0, 0, 0, 0, 1.58113883008419, 0, 1.73205080756888,
0, 0, 0, 0, 0, 1.73205080756888, 0, 1.73205080756888, 0, 0, 0,
0, 0, 1.73205080756888, 0, 1.58113883008419, 0, 0, 0, 0, 0, 1.58113883008419,
0, 1.22474487139159, 0, 0, 0, 0, 0, 1.22474487139159, 0), .Dim = c(7L,
7L))
I figured out a solution with a little help from my friend. Since, Spin_Sx is a symmetric matrix (real or complex), it can be diagonalized as: Spin_Sx = UDU', where U is a unitary matrix, D is a diagonal matrix, and UU' = I. Therefore, exp(Spin_Sx) = U exp(D) U'. As D is a diagonal matrix, the exp(D) is simply the element exponential of the diagonal elements of D.
> A <- 1i*(pi/2)*Spin_Sx
> eig_Values <- eigen(A)$values
> Diagonal_A <- diag(exp(eig_Values))
> eig_Vector <- eigen(A)$vector
> expm_A <- eig_Vector %*% Diagonal_A %*% t(Conj(eig_Vector))
I'd like to melt the dataframe so that in one column I have dates and in a second I have username as the variable and finally the value.
I'm getting this error:
Error in as.Date.numeric(value) : 'origin' must be supplied
and while I understand the error I'm not exactly sure how to get around it.
A small sample of the data is:
structure(list(created_at = structure(c(14007, 14008, 14009,
14010, 14011, 14012), class = "Date"), benjamin = c(16, 0, 0,
0, 0, 0), byron = c(0, 0, 0, 0, 0, 0), cameronc = c(0, 0, 0,
0, 0, 0), daniel = c(0, 0, 0, 0, 0, 0), djdiaz = c(0, 0, 0, 0,
0, 0), gene = c(16, 77, 64, 38, 72, 36), joel = c(0, 0, 0, 0,
0, 2), kerem = c(0, 0, 0, 0, 0, 0), sophia = c(0, 0, 0, 0, 0,
0), SuperMoonMan = c(0, 0, 0, 0, 0, 0)), .Names = c("created_at",
"benjamin", "byron", "cameronc", "daniel", "djdiaz", "gene",
"joel", "kerem", "sophia", "SuperMoonMan"), row.names = c(NA,
6L), class = c("cast_df", "data.frame"))
Thanks for your help.
Try converting the created_at variable into a character vector. melt also doesn't seem to like the cast_df class, but I had success by resetting the class to just data.frame. Like so:
df <- as.data.frame(df)
df$created_at <- as.character(df$created_at)
library(reshape)
melt(df)
You error is caused by rbind used in melt, which is consequence of wrong data to melt. I don't know how you create your cast_df data.frame, but it missing attributes (idvars and rdimnames) which are required by melt.cast_df.
That is why wkmor1 solution works, melt.data.frame don't need this arguments. And without converting Date to character it can be done as:
df <- as.data.frame(df)
melt(df, id="created_at")