This question already has answers here:
How to combine all rows into a single row in R? [closed]
(2 answers)
Closed 6 years ago.
Got a basic R for-loop matrix question:
My matrix looks something like this:
2 4 3
1 5 7
All I want is to print these elements row wise and not column wise. The answer should be like 2 4 3 1 5 7. All I try I get the result column wise i.e `2 1 4 5 3 7. Since m just beginning R wondering if it can be done by just for-loop which loops column wise and not row-wise
m <- matrix(c(2, 4, 3, 1, 5, 7), 2, 3, byrow=T)
as.vector(m)
or
c(m[1, 1:3], m[2, 1:3])
Related
This question already has answers here:
Creating co-occurrence matrix
(5 answers)
Closed 3 months ago.
I have a R dataframe that consists of two columns, id and text, and I want to turn it into a cooccurrence matrix of word pairs that appear together in the same id's list of words.
So, this dataframe:
df <- data.frame(id = c(1, 1, 1, 2, 2, 2), text = c(but, the, and, but, a, the))
should be turned into something like this:
but
the
and
a
but
2
2
1
1
the
2
2
1
1
and
1
1
1
0
a
1
1
0
1
But at larger scale. I think this toy example should be transferable though. I'm not sure where to even start here, but tidyverse solutions are preferred.
Following this answer:
dat <- crossprod(table(df))
This question already has answers here:
Count observations greater than a particular value [duplicate]
(3 answers)
Closed 2 years ago.
I have a list
x <- c(1,2,3,4,5)
How do I count how many elements are over 3. So the output would be 2.
Take advantage of the fact logical are represented by 1 and 0
sum(1:5 > 3)
You can do like this:
x <- list(1, 2, 3, 4, 5)
sum( x > 3 )
#Output: [1] 2
This question already has answers here:
Remove rows with all or some NAs (missing values) in data.frame
(18 answers)
Closed 3 years ago.
I have two vectors, say A and B
A <- c(1, 2, 3, 4, 5)
B <- c(6, NA, 8, 9, NA)
I would like to exclude elements in A corresponding to the elements of B which comprise NAs.
So, I am in need of an automatic way to remove indices 2 and 5 from both A and B, so that the length of both vectors is the same.
Use is.na
A[!is.na(B)]
#[1] 1 3 4
B[!is.na(B)]
#[1] 6 8 9
Something like
na.omit(cbind(A,B))
This question already has answers here:
Numbering rows within groups in a data frame
(10 answers)
Closed 5 years ago.
If I have a vector of numbers in R.
numbers <- c(1,1, 2,2,2, 3,3, 4,4,4,4, 1)
I want to return a vector that provides the number of times that value has occurred cumulatively along the vector. I.e.
results <- c(1,2, 1,2,3, 1,2, 1,2,3,4, 3)
We can use ave and apply the seq_along by grouping with the 'numbers' vector
ave(numbers, numbers, FUN = seq_along)
#[1] 1 2 1 2 3 1 2 1 2 3 4 3
This question already has answers here:
Calculating cumulative sum for each row
(6 answers)
Closed 7 years ago.
I've come across the aggregate() function and things like seq_along(), but I'm not sure how to solve this yet:
For the following:
x
1
5
10
20
I'd like to get the following output:
y
1
6
16
36
It seemed to me that doing something like x[1:seq_along(x)] would do the trick but it seems not because seq_along(x) is a sequence rather than a number.
As mentioned by #DavidArenburg, you can use the cumsum function:
x <- c(1, 5, 10, 20)
cumsum(x)
[1] 1 6 16 36