r - Collapse multiple rows in one following multiple conditions with tidyr [duplicate] - r

This question already has answers here:
How to sum a variable by group
(18 answers)
Closed 5 years ago.
i have a database structure like this
A B C
n 1 M
n 2 U
n 1 U
f 3 M
f 4 M
f 1 U
using the package tidyr, I want to obtain this result:
A B C
n 1 M
n 3 U
f 7 M
f 1 U
So I want to make a sum of the b value characterized by the same A value and, obtained this sub set, collapsing B value in relation to the same C value.
How could I do?

library(dplyr)
df %>%
group_by(A,C) %>%
summarize(B=sum(B)) %>%
data.frame()

Related

Select a category in dataframe for operation in r? [duplicate]

This question already has answers here:
Conditional replacement of values in a data.frame
(5 answers)
Closed last year.
Having this dataframe:
dat=data.frame(a=c("ll","pp","ml","ml","v"),value=c(1,2,12,1,2))
I want to multiply by 10 only values correspond to a=ml
In base R:
dat=data.frame(a=c("ll","pp","ml","ml","v"),value=c(1,2,12,1,2))
dat$value[dat$a=="ml"] = dat$value[dat$a=="ml"] * 10
dat
Output:
a value
1 ll 1
2 pp 2
3 ml 120
4 ml 10
5 v 2
Another solution is to use a ifelse statement
dat %>%
mutate(value = ifelse(a == "ml", value*10, value))
a value
1 ll 1
2 pp 2
3 ml 120
4 ml 10
5 v 2

dplyr unique occurrence count on columns [duplicate]

This question already has answers here:
Counting unique / distinct values by group in a data frame
(12 answers)
How to count the number of unique values by group? [duplicate]
(1 answer)
Closed 5 years ago.
I want to get the number of unique values from one column grouped by another column using dplyr. Preferable function friendly, that is i can put this in a function and it will work easily.
So for example for the following data frame.
test = data.frame(one=rep(letters[1:5],each=2), two=c(rep("c", 3), rep("d", 2), rep("e", 4), "f") )
one two
1 a c
2 a c
3 b c
4 b d
5 c d
6 c e
7 d e
8 d e
9 e e
10 e f
I would want something like the number of unique values column two gives column one.
Desired output:
one n
1 a 1
2 b 2
3 c 2
4 d 1
5 e 2
From column one, a has 1 unique value "c" only, b has 2 unique value "c" and "d", c has 2 unique values "d" and "e", d has 1 unique value "e".
I managed to get something working by group_by() twice and summarize(), is there a more simple way i could use?
Hope this is understandable.
Thanks
We can group by 'one' and get the number of unique elements with n_distinct
library(dplyr)
test %>%
group_by(one) %>%
summarise(n = n_distinct(two))

convert values of a column based on another dataframe in R [duplicate]

This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 6 years ago.
I have a data.frame
df1=data.frame(f=LETTERS[1:4],v=c(1:4))
f v
1 A 1
2 B 2
3 C 3
4 D 4
The first column is a list of factors, in which I have another data frame that houses these values, which are also factors
df2=data.frame(f=LETTERS[1:7],f2=letters[26:20])
f f2
1 A z
2 B y
3 C x
4 D w
5 E v
6 F u
I am wondering how to write a function so that I can alter the values from the first column of df1 to what they map to from df2. I would like to get:
f v
1 z 1
2 y 2
3 x 3
4 w 4
I tried a for loop with no success. Ant suggestions is greatly appreciated
Note: this is a simplified example of my work. A merge would add too many columns to work with and I don't think the extra memory storage would be very useful
We can use match
df1$f <- df2$f2[match(df1$f, df2$f)]
df1
# f v
#1 z 1
#2 y 2
#3 x 3
#4 w 4
You can use merge
merge(df1,df2,by = "f")[,c(1,3,2)]
f f2 v
1 A z 1
2 B y 2
3 C x 3
4 D w 4
library(dplyr)
left_join(df1,df2)
You could try using the merge function to merge the two tables, then specify which columns you want to keep.
For example:
df1 <- data.frame(f=LETTERS[1:4],v=c(1:4))
df2 <- data.frame(f=LETTERS[1:7],f2=letters[26:20])
merge(df1, df2, by.x = "f")[,c("f2", "v")]
f2 v
1 z 1
2 y 2
3 x 3
4 w 4

Convert from n x m matrix to long matrix in R [duplicate]

This question already has answers here:
Create dataframe from a matrix
(6 answers)
Closed 1 year ago.
Note: This is not a graph question.
I have an n x m matrix:
> m = matrix(1:6,2,3)
> m
a b c
d 1 2 3
e 4 5 6
I would like to convert this to a long matrix:
> m.l
a d 1
a e 4
b d 2
b e 5
c d 3
c e 6
Obviously nested for loops would work but I know there are a lot of nice tools for reshaping matrixes in R. So far, I have only found literature on converting from long or wide matrixes to an n x m matrix and not the other way around. Am I missing something obvious? How can I do this conversion?
Thank you!
If you need a single column matrix
matrix(m, dimnames=list(t(outer(colnames(m), rownames(m), FUN=paste)), NULL))
# [,1]
#a d 1
#a e 4
#b d 2
#b e 5
#c d 3
#c e 6
For a data.frame output, you can use melt from reshape2
library(reshape2)
melt(m)

Cross matrix values in R [duplicate]

This question already has answers here:
How to calculate combination and permutation in R?
(6 answers)
Closed 8 years ago.
Consider I have a list of products : For example 100 product names
Product$list
A
B
C
I want 100 * 100 combination in Product$list1 and Product$list2
Example:3*3 = 9 combinations
Product$list1 Product$list2
A B
A C
B A
B C
C A
C B
A A
B B
C C
Can somebody help me how to achieve this in R.
You could try combn
setNames(as.data.frame(t(combn(Product$Col,2))), paste0("Col",1:2))
data
Product <- data.frame(Col=LETTERS[1:3], stringsAsFactors=FALSE)

Resources