extract and format data from dataset into matrix in R - r
I want to make this dataframe
into this matrix
I have tried:
x <- read.csv("sample1.csv")
ax <- matrix(c(x[1,1],x[2,1],x[1,3],x[1,1],x[3,1],x[1,4],x[1,1],x[4,1],x[1,5],x[1,1],x[5,1],x[1,6],x[1,1],x[6,1],x[1,7],x[2,1],x[1,1],x[2,2],x[2,1],x[3,1],x[2,4],x[2,1],x[4,1],x[2,5],x[2,1],x[5,1],x[2,6],x[3,1],x[6,1],x[2,7],x[3,1],x[1,1],x[3,2],x[3,1],x[2,1],x[3,3],x[3,1],x[4,1],x[3,5],x[3,1],x[5,1],x[3,6],x[3,1],x[6,1],x[3,7],x[4,1],x[1,1],x[4,2],x[4,1],x[2,1],x[4,3],x[4,1],x[3,1],x[4,4],x[4,1],x[5,1],x[4,6],x[4,1],x[6,1],x[4,7],x[5,1],x[1,1],x[2,2],x[5,1],x[2,1],x[2,4],x[5,1],x[3,1],x[2,5],x[5,1],x[4,1],x[2,6],x[5,1],x[6,1],x[2,7],x[6,1],x[1,1],x[2,2],x[6,1],x[2,1],x[2,4],x[6,1],x[3,1],x[2,5],x[6,1],x[4,1],x[2,6],x[6,1],x[5,1],x[2,7]),10,3, byrow=TRUE)
bx <- ax[order(ax[,3], decreasing = TRUE),]
But it's not beautiful at all, and also it's gonna be lots of work if I got different sample data.
So I wish to simplified it if possible, any suggestion?
This can be achieved by using melt() function from reshape2 package:
> a = matrix(c(1:9), nrow = 3, ncol = 3, dimnames = list(LETTERS[1:3], letters[1:3]))
> a
a b c
A 1 4 7
B 2 5 8
C 3 6 9
> library(reshape2)
> melt(a, na.rm = TRUE)
Var1 Var2 value
1 A a 1
2 B a 2
3 C a 3
4 A b 4
5 B b 5
6 C b 6
7 A c 7
8 B c 8
9 C c 9
Related
R - Sum list of matrix with different columns
I have a large list of matrix with different columns and I would like to sum these matrix counting 0 if column X does not exist in one matrix. If you have used the function rbind.fill from plyr I would like something similar but with sum function. Of course I could build a function to do that, but I'm thinking about a native function efficiently programmed in Frotrain or C due to my large data. Here an example: This is the easy example where I have the same columns: aa <- list( m1 = matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, dimnames = list(c(1,2,3),c('a','b','c'))), m2 = matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, dimnames = list(c(1,2,3),c('a','b','c'))) ) aa Reduce('+',aa) Giving the results: > aa $m1 a b c 1 1 4 7 2 2 5 8 3 3 6 9 $m2 a b c 1 1 4 7 2 2 5 8 3 3 6 9 > Reduce('+',aa) a b c 1 2 8 14 2 4 10 16 3 6 12 18 And with my data: bb <- list( m1 = matrix(c(1,2,3,7,8,9), nrow = 3, dimnames = list(c(1,2,3),c('a','c'))), m2 = matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, dimnames = list(c(1,2,3),c('a','b','c'))) ) bb Reduce('+',bb) Here I would like to have b = c(0,0,0) in the first matrix to sum them. > bb $m1 a c 1 1 7 2 2 8 3 3 9 $m2 a b c 1 1 4 7 2 2 5 8 3 3 6 9 Many thanks! Xevi
One option would be un1 <- sort(unique(unlist(lapply(bb, colnames)))) bb1 <- lapply(bb, function(x) { nm1 <- setdiff(un1, colnames(x)) m1 <- matrix(0, nrow = nrow(x), ncol = length(nm1), dimnames = list(NULL, nm1)) cbind(x, m1)[, un1]}) and use the Reduce Reduce(`+`, bb1) # a b c # 1 2 4 14 # 2 4 5 16 # 3 6 6 18
Convert a matrix to columns
Assuming I have a matrix looks like below, the values up or down the diagonal are the same. In other words, [,1] x [2,] and [,2] x [1,] both are 2 in the matrix. > m = cbind(c(1,2,3),c(2,4,5),c(3,5,6)) > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 4 5 [3,] 3 5 6 Then I have real name for 1, 2, and 3 as well. >Real_name A B C # A represents 1, B represents 2, and C represents 3. If I would like to convert the matrix to 3 columns containing corresponding real name for each pair, and the pair must be unique, A x B is the same as B x A, so we keep A x B only. How can I achieve it using R? A A 1 A B 2 A C 3 B B 4 B C 5 C C 6
The following is straightforward: m <- cbind(c(1,2,3), c(2,4,5), c(3,5,6)) ## read `?lower.tri` and try `v <- lower.tri(m, diag = TRUE)` to see what `v` is ## read `?which` and try `which(v, arr.ind = TRUE)` to see what it gives ij <- which(lower.tri(m, diag = TRUE), arr.ind = TRUE) Real_name <- LETTERS[1:3] data.frame(row = Real_name[ij[, 1]], col = Real_name[ij[, 2]], val = c(m[ij])) # row col val #1 A A 1 #2 B A 2 #3 C A 3 #4 B B 4 #5 C B 5 #6 C C 6
colnames(m) <- c("A", "B", "C") rownames(m) <- c("A", "B", "C") m[lower.tri(m)] = NA # replace lower triangular elements with NA data.table::melt(m, na.rm = TRUE) # melt and remove NA # Var1 Var2 value #1 A A 1 #4 A B 2 #5 B B 4 #7 A C 3 #8 B C 5 #9 C C 6 Or you can do it in a single line: melt(replace(m, lower.tri(m), NA), na.rm = TRUE)
This will also work: g <- expand.grid(1:ncol(m), 1:ncol(m)) g <- g[g[,2]>=g[,1],] cbind.data.frame(sapply(g, function(x) Real_name[x]), Val=m[as.matrix(g)]) Var1 Var2 Val 1 A A 1 2 A B 2 3 B B 4 4 A C 3 5 B C 5 6 C C 6
Get a subset not containing a given value of the column
I have a table called data: A 22 B 333 C Not Av. D Not Av. How can I get a subset, from which all rows containing "Not Av." are excluded? It is important to mention that I have the index of a column to be checked (in this case colnum = 2), but I don't have its name. I tried this, but it does not work: data<-subset(data,colnum!="Not Available")
df <- read.csv(text="A,22 B,333 C,Not Av. D,Not Av.", header=F) df[df[,2] != "Not Av.",]
You don't really need the subset function. Just use [: > set.seed(42) > DF <- data.frame(x = LETTERS[1:10], y = sample(c(1, 2, 3, "Not Av."), 10, replace = TRUE)) > DF x y 1 A Not Av. 2 B Not Av. 3 C 2 4 D Not Av. 5 E 3 6 F 3 7 G 3 8 H 1 9 I 3 10 J 3 > DF[DF[2] != "Not Av.",] x y 3 C 2 5 E 3 6 F 3 7 G 3 8 H 1 9 I 3 10 J 3
In case you still want to use the subset function: df<-subset(df,!grepl("Not Av",df[,2]))
Replacing header in data frame based on values in second data frame
Say I have a data frame which looks like this: df.A A B C x 1 3 4 y 5 4 6 z 8 9 1 And I want to replace the column names in the first based on column values in a second: df.B Low High A D B F C G Such that I get: df.A D F G x 1 3 4 y 5 4 6 z 8 9 1 How would I do it? I have tried extracting the vector df.B$High from df.B and using this in names(df.A), but everything is in alphabetical order and shifted over one. Furthermore, this only works if the order of columns in df.A is conserved with respect to the elements in df.B$High, which is not always the case (and in my real example there is no numeric or alphabetical way to sort the two to the same order). So I think I need an rbind-type argument for matching elements, but I'm not sure. Thanks!
You can use rename from plyr: library(plyr) dat <- read.table(text = " A B C x 1 3 4 y 5 4 6 z 8 9 1",header = TRUE,sep = "") > new <- read.table(text = "Low High A D B F C G",header = TRUE,sep = "") > rename(dat,replace = setNames(new$High,new$Low)) D F G x 1 3 4 y 5 4 6 z 8 9 1
using match: df.A <- read.table(sep=" ", header=T, text=" A B C x 1 3 4 y 5 4 6 z 8 9 1") df.B <- read.table(sep=" ", header=T, text=" Low High A D B F C G") df.C <- df.A names(df.C) <- df.B$High[match(names(df.A), df.B$Low)] df.C # D F G # x 1 3 4 # y 5 4 6 # z 8 9 1
You can play games with the row names of df.B to make a lookup more convenient: rownames(df.B) <- df.B$Low names(df.A) <- df.B[names(df.A),"High"] df.A ## D F G ## x 1 3 4 ## y 5 4 6 ## z 8 9 1
Here's an approach abusing factor: f <- factor(names(df.A), levels=df.B$Low) levels(f) <- df.B$High f ## [1] D F G ## Levels: D F G names(df.A) <- f ## Desired results
change data.frame column into rows in R
A <- c(1,6) B <- c(2,7) C <- c(3,8) D <- c(4,9) E <- c(5,0) df <- data.frame(A,B,C,D,E) df A B C D E 1 1 2 3 4 5 2 6 7 8 9 0 I would like to have this: df 1 2 A 1 6 B 2 7 C 3 8 D 4 9 E 5 0
If your dataframe is truly in that format, then all of your vectors will be character vectors. Or, you basically have a character matrix and you could do this: data.frame(t(df)) It would be better, though, to just define it the way you want it from the get-go df <- data.frame(c('A','B','C','D','E'), c(1, 2, 3, 4, 5), c(6, 7, 8, 9, 0)) You could also do this df <- data.frame(LETTERS[1:5], 1:5, c(6:9, 0)) If you wanted to give the columns names, you could do this df <- data.frame(L = LETTERS[1:5], N1 = 1:5, N2 = c(6:9, 0)) Sometimes, if I use read.DIF of Excel data the data gets transposed. Is that how you got the original data in? If so, you can call read.DIF(filename, transpose = T) to get the data in the correct orientation.
I really recommend data.table approach without manual steps becauce they are error-prone A <- c(1,6) B <- c(2,7) C <- c(3,8) D <- c(4,9) E <- c(5,0) df <- data.frame(A,B,C,D,E) df library('data.table') dat.m <- melt(as.data.table(df, keep.rownames = "Vars"), id.vars = "Vars") # https://stackoverflow.com/a/44128640/54964 dat.m Output A B C D E 1 1 2 3 4 5 2 6 7 8 9 0 Vars variable value 1: 1 A 1 2: 2 A 6 3: 1 B 2 4: 2 B 7 5: 1 C 3 6: 2 C 8 7: 1 D 4 8: 2 D 9 9: 1 E 5 10: 2 E 0 R: 3.4.0 (backports) OS: Debian 8.7