Sum of cells with same row and column name in R - r

I have a matrix created using table() command in R in which rows and columns do not have same values.
0 1 2
1 1 2 3
2 4 5 6
3 7 7 8
How can I sum the elements with the same row and column name? In this example it is equal to (2+6=)8.

Here's one approach:
# find the values present in both row names and column names
is <- do.call(intersect, unname(dimnames(x)))
# calculate the sum
sum(x[cbind(is, is)])
where x is your table.

Another one, self-explanatory:
sum(x[colnames(x)[col(x)] == rownames(x)[row(x)]])

Related

if i want to sort a column by size in rstudio, how do i make sure that the associated values of the rows sort with the column?

I have a data.frame with 1200 rows and 5 columns, where each row contains 5 values of one person. now i need to sort one column by size but I want the remaining columns to sort with the column, so that one column is sorted by increasing values and the other columns contain the values of the right persons. ( So that one row still contains data from one and the same person)
colnames(BAPlotDET) = c("fsskiddet", "fspiddet","avg", "diff","absdiff")
these are the column names of my data.frame and I wanna sort it by the column called "avg"
First of all, please always provide us with a reproducible example such as below. The sorting of a data frame by default sorts all columns.
vector <- 1:3
BAPlotDET <- data.frame(vector, vector, vector, vector, vector)
colnames(BAPlotDET) = c("fsskiddet", "fspiddet","avg", "diff","absdiff")
fsskiddet fspiddet avg diff absdiff
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
BAPlotDET <- BAPlotDET[order(-BAPlotDET$avg),]
> BAPlotDET
fsskiddet fspiddet avg diff absdiff
3 3 3 3 3 3
2 2 2 2 2 2
1 1 1 1 1 1

For each row return the multiple column indexs for specific number

Hi suppose I have a matrix with 0 an 1 only and I want to find out where 1 locates in each row. And for each row, there are multiple 1 exist.
For example I have
set.seed(444)
m3 <- matrix(round(runif(8*8)), 8,8)
For the first row I have column 2,3,8 are 1 and I want a code could report either column name or column index. Meanwhile, it is worth to point out that each the number of 1 in each row could be different.
Can anyone provide some suggestions? I appreciate it so much.
We can use which with arr.ind which returns the row/column index as a matrix
out <- which(m3 ==1, arr.ind = TRUE)
out[,2][order(out[,1])]
[1] 2 3 8 3 5 3 4 8 7 4 6 7 1 3 4 6 1 4 5 6 7 2 4 7 8
To get the column name, use the same index (if the matrix have any column names- here there are not column names attribute)
colnames(m3)[out[,2][order(out[,1])]]

Compare columns from two data sets

I have two data sets
1 10
2 15
3 17
4 5
The second
1 to
2 b
4 c
I need to compare only one column of the two sets and at the end have what values are equal to the two sets in that column and exclude the rest, in summary when comparing the two columns I would have the following result
1 10
2 15
4 5
I don't know where to start, if someone can help me get started
We can use subset from base R (if the first column name in both datasets are 'col1')
subset(df1, col1 %in% df2$col1)

How to create new dataframe by repeating 1 column and sequentially repeating a 2nd column of original dataframe?

Im trying to create a new dataframe by repeating values in original-df column 1 and corresponding them to repeating values from original-df column 2. However, the values should repeat in a different manner for each column. For example, values from original-df column 1 will repeat as 1,2,3,1,2,3,1,2,3. Where as values from original-df column 2 should repeat as 1,1,1,2,2,2,3,3,3.
#here is original df
df1<-data.frame(x=1:3, y=10:12)
#I've tried the followig:
data.frame(x=df1$x,y=df1[,2])->df2
range<-1:3
data.frame(x=df1$x,y=df1$y[range,2])->df3
#I then tried this:
rep(df1$x,df1$y[l,2])->df4
#output either looks like this:
x y
1 1 10
2 2 11
3 3 12
#Or I receive an error message:
Error in df1$y[1, 2] : incorrect number of dimensions
#I expect data output to look like this:
x y
1 10
2 10
3 10
1 11
2 11
3 11
1 12
2 12
3 12
An option would be expand
library(tidyr)
expand(df1, x, y)
Or with expand.grid from base R
do.call(expand.grid, df1)

R: How can I remove rows from all the data frames in this list?

Say I have some data created like this
n <- 3
K <- 4
dat <- expand.grid(var1=1:n, var2=1:K)
dat looks like this:
var1 var2
1 1 1
2 2 1
3 3 1
4 1 2
5 2 2
6 3 2
7 1 3
8 2 3
9 3 3
10 1 4
11 2 4
12 3 4
I want to remove some rows from both data frames in the list at the same time. Let's say I want to remove the 11th row, and I want the 'gap' to be filled in, so that now the 12th row will become the 11th row.
I understand this is a list of two data frames. Thus the advice here does not apply, since dat[[11]]<-NULL would do nothing, while dat[[2]]<-NULL would remove the second data frame from the list
lapply(dat,"[",11) lets me access the relevant elements, but I don't know how to remove them.
Assuming that we want to remove rows from a list of data.frames, we loop the list elements using lapply and remove the rows using numeric index.
lapply(lst, function(x) x[-11,])
Or without the anonymous function
lapply(lst, `[`, -11,)
The 'dat' is a data.frame.
is.data.frame(dat)
#[1] TRUE
If we want to remove rows from 'dat',
dat[-11,]
If the row.names also needs to be changed
`row.names<-`(dat[-11,], NULL)
data
lst <- list(dat, dat)

Resources