Summarize data frame based on condition - r

I have this kind of dataset (ID, V1, V2 are the 3 variables of my data frame):
ID V1 V2
1 A 10
1 B 5
1 D 1
2 C 9
2 E 8
I would like a new data frame with, for each ID, the line that has the value max in V2. For the example, the result would be:
ID V1 V2
1 A 10
2 C 9

Use ddply from plyr package (assume data is sample)
library(plyr)
ddply(sample,.(ID),summarize,V1=V1[which.max(V2)],V2=max(V2))
ID V1 V2
1 1 A 10
2 2 C 9

This is sort of clumsy code, but it works....
> mydf[with(mydf, ave(V2, ID, FUN = function(x) x == max(x))) == 1, ]
ID V1 V2
1 1 A 10
4 2 C 9
Less clumsy:
do.call(rbind,
by(mydf, mydf$ID,
FUN = function(x) x[which.max(x$V2), ]))
# ID V1 V2
# 1 1 A 10
# 2 2 C 9

Related

R function to replace tricky merge in Excel (vlookup + hlookup)

I have a tricky merge that I usually do in Excel via various formulas and I want to automate with R.
I have 2 dataframes, one called inputs looks like this:
id v1 v2 v3
1 A A C
2 B D F
3 T T A
4 A F C
5 F F F
And another called df
id v
1 1
1 2
1 3
2 2
3 1
I would like to combined them based on the id and v values such that I get
id v key
1 1 A
1 2 A
1 3 C
2 2 D
3 1 T
So I'm matching on id and then on the column from v1 thru v2, in the first example you will see that I match id = 1 and v1 since the value of v equals 1. In Excel I do this combining creatively VLOOKUP and HLOOKUP but I want to make this simpler in R. Dataframe examples are simplified versions as the I have more records and values go from v1 thru up to 50.
Thanks!
You could use pivot_longer:
library(tidyr)
library(dplyr)
key %>% pivot_longer(!id,names_prefix='v',names_to = 'v') %>%
mutate(v=as.numeric(v)) %>%
inner_join(df)
Joining, by = c("id", "v")
# A tibble: 5 × 3
id v value
<int> <dbl> <chr>
1 1 1 A
2 1 2 A
3 1 3 C
4 2 2 D
5 3 1 T
Data:
key <- read.table(text="
id v1 v2 v3
1 A A C
2 B D F
3 T T A
4 A F C
5 F F F",header=T)
df <- read.table(text="
id v
1 1
1 2
1 3
2 2
3 1 ",header=T)
You can use two column matrices as index arguments to "[" so this is a one liner. (Not the names of the data objects are d1 and d2. I'd opposed to using df as a data object name.)
d1[-1][ data.matrix(d2)] # returns [1] "A" "A" "C" "D" "T"
So full solution is:
cbind( d2, key= d1[-1][ data.matrix(d2)] )
id v key
1 1 1 A
2 1 2 A
3 1 3 C
4 2 2 D
5 3 1 T
Try this:
x <- "
id v1 v2 v3
1 A A C
2 B D F
3 T T A
4 A F C
5 F F F
"
y <- "
id v
1 1
1 2
1 3
2 2
3 1
"
df <- read.table(textConnection(x) , header = TRUE)
df2 <- read.table(textConnection(y) , header = TRUE)
key <- c()
for (i in 1:nrow(df2)) {
key <- append(df[df2$id[i],(df2$v[i] + 1L)] , key)
}
df2$key <- rev(key)
df2
># id v key
># 1 1 1 A
># 2 1 2 A
># 3 1 3 C
># 4 2 2 D
># 5 3 1 T
Created on 2022-06-06 by the reprex package (v2.0.1)

How to sort columns of several data frames in a list?

I have a list in R, with several data frames having timestamp as a column. I want to sort this column in all of them. How can I do it?
in base R
L <- list(data.frame(v1 = 3:1, v2 = 1:3),
data.frame(v1 = 6:4, v2 = 4:6))
# [[1]]
# v1 v2
# 1 3 1
# 2 2 2
# 3 1 3
#
# [[2]]
# v1 v2
# 1 6 4
# 2 5 5
# 3 4 6
lapply(L, function(x) x[order(x$v1), ])
# [[1]]
# v1 v2
# 3 1 3
# 2 2 2
# 1 3 1
#
# [[2]]
# v1 v2
# 3 4 6
# 2 5 5
# 1 6 4
Using tidyverse - loop over the list with map (from purrr) and arrange the rows based on the 'timestamp' column
library(purrr)
library(dplyr)
lst2 <- map(lst1, ~ .x %>%
arrange(timestamp))

R Ifelse: Find if any column meet the condition

I'm trying to apply the same condition for multiple columns of an array and, then, create a new column if any of the columns meet the condition.
I can do it manually with an OR statement, but I was wondering if there is an easy way to apply it for more columns.
An example:
data <- data.frame(V1=c("A","B"),V2=c("A","A","A","B","B","B"),V3=c("A","A","B","B","A","A"))
data[4] <- ifelse((data[1]=="A"|data[2]=="A"|data[3]=="A"),1,0)
So the 4th row is the only that doesn't meet the condition for all columns:
V1 V2 V3 V1
1 A A A 1
2 B A A 1
3 A A B 1
4 B B B 0
5 A B A 1
6 B B A 1
Do you know a way to apply the condition in a shorter code?
I tried something like
data[4] <- ifelse(any(data[,c(1:3)]=="A"),1,0)
but it consider the condition for all the dataset instead of by rows, so all the rows are given 1.
We can use Reduce with lapply
data$NewCol <- +( Reduce(`|`, lapply(data, `==`, 'A')))
We can use apply row-wise :
data$ans <- +(apply(data[1:3] == "A", 1, any))
data
# V1 V2 V3 ans
#1 A A A 1
#2 B A A 1
#3 A A B 1
#4 B B B 0
#5 A B A 1
#6 B B A 1
Try:
data$V4 <- +(rowSums(data == 'A') > 0)
Output:
V1 V2 V3 V4
1 A A A 1
2 B A A 1
3 A A B 1
4 B B B 0
5 A B A 1
6 B B A 1

How do I stack only some columns in a data frame?

I have some data in a data frame in the following form:
A B C V1 V2 V3
1 1 1 x y z
1 1 2 a b c
...
Where A,B,C are factors, and the combination A,B,C is unique for each row.
I need to convert some of the columns into factors, to achieve a form like:
A B C V val
1 1 1 V1 x
1 1 1 V2 y
1 1 1 V3 z
1 1 2 V1 a
1 1 2 V2 b
1 1 2 V2 c
...
This seems to relate to both stack and the inverse of xtabs, but I don't see how to specify that only certain columns should be "stacked".
And before #AnandaMahto gets here and offers his base reshape solution, here's my attempt:
dat <- read.table(text = 'A B C V1 V2 V3
1 1 1 x y z
1 1 2 a b c',header= T)
expandvars <- c("V1","V2","V3")
datreshape <- reshape(dat,
idvar=c("A","B","C"),
varying=list(expandvars),
v.names=c("val"),
times=expandvars,
direction="long")
> datreshape
A B C time val
1.1.1.V1 1 1 1 V1 x
1.1.2.V1 1 1 2 V1 a
1.1.1.V2 1 1 1 V2 y
1.1.2.V2 1 1 2 V2 b
1.1.1.V3 1 1 1 V3 z
1.1.2.V3 1 1 2 V3 c
Using reshape2 package
dat <- read.table(text = 'A B C V1 V2 V3
1 1 1 x y z
1 1 2 a b c',header= T)
library(reshape2)
melt(dat,id.vars = c('A','B','C'))
A B C variable value
1 1 1 1 V1 x
2 1 1 2 V1 a
3 1 1 1 V2 y
4 1 1 2 V2 b
5 1 1 1 V3 z
6 1 1 2 V3 c
stack
You are right that stack is a possibility, but you perhaps missed a key line in the documentation for stack:
Note that stack applies to vectors (as determined by is.vector): non-vector columns (e.g., factors) will be ignored (with a warning as from R 2.15.0).
So, how do we proceed?
Here's your data:
dat <- read.table(text = 'A B C V1 V2 V3
1 1 1 x y z
1 1 2 a b c',header= T)
Here, we convert the factors to as.character:
dat[sapply(dat, is.factor)] = lapply(dat[sapply(dat, is.factor)], as.character)
Here's how we specify which columns to stack:
stack(dat[4:6])
# values ind
# 1 x V1
# 2 a V1
# 3 y V2
# 4 b V2
# 5 z V3
# 6 c V3
But, we still need to "expand" your rows for columns 1-3. See here for how to do that.
With this information, we can use cbind to get the desired result.
cbind(dat[rep(row.names(dat), 3), 1:3], stack(dat[4:6]))
# A B C values ind
# 1 1 1 1 x V1
# 2 1 1 2 a V1
# 1.1 1 1 1 y V2
# 2.1 1 1 2 b V2
# 1.2 1 1 1 z V3
# 2.2 1 1 2 c V3
xtabs
You are also right that xtabs seems like it could be a likely possibility, but xtabs actually expects the opposite of what you've provided. That is to say, when you specify a formula, it expects the items on the left hand side to be numbers, and the items on the right hand side to be factors. Thus, is your data were swapped, you could certainly use xtabs.
Here's a demonstration (which only works because you are using a simple example where we can easily match "letters" to "numbers").
dat2 <- dat # Make a copy of "dat"
# Swap out dat 4-6 with numbers
dat2[4:6] <- lapply(dat2[4:6], function(x) match(x, letters))
# Swap out dat 1-3 with letters
dat2[1:3] <- lapply(dat2[1:3], function(x) letters[x])
# Our new "dat"
dat2
# A B C V1 V2 V3
# 1 a a a 24 25 26
# 2 a a b 1 2 3
data.frame(xtabs(cbind(V1, V2, V3) ~ A + B + C, dat2))
# A B C Var4 Freq
# 1 a a a V1 24
# 2 a a b V1 1
# 3 a a a V2 25
# 4 a a b V2 2
# 5 a a a V3 26
# 6 a a b V3 3
In other words, your choice of tools could potentially be right, but your data needs to also be in the form that the tools expect.
But, I'm not sure why you'd want to do all the work I've shown when better solutions exist with reshape and friends ;)
Very late update...
You can also look at merged.stack from my "splitstackshape" package:
library(splitstackshape)
merged.stack(dat, var.stubs = "V", sep = "NoSep")
# A B C .time_1 V
# 1: 1 1 1 V1 x
# 2: 1 1 1 V2 y
# 3: 1 1 1 V3 z
# 4: 1 1 2 V1 a
# 5: 1 1 2 V2 b
# 6: 1 1 2 V3 c
Or gather from "tidyr":
library(dplyr)
library(tidyr)
# gather(dat, var, val, V1:V3)
dat %>% gather(var, val, V1:V3)
# A B C var val
# 1 1 1 1 V1 x
# 2 1 1 2 V1 a
# 3 1 1 1 V2 y
# 4 1 1 2 V2 b
# 5 1 1 1 V3 z
# 6 1 1 2 V3 c

melt data frame and split values

I have the following data frame with measurements concatenated into a single column, separated by some delimiter:
df <- data.frame(v1=c(1,2), v2=c("a;b;c", "d;e;f"))
df
v1 v2
1 1 a;b;c
2 2 d;e;f;g
I would like to melt/transforming it into the following format:
v1 v2
1 1 a
2 1 b
3 1 c
4 2 d
5 2 e
6 2 f
7 2 g
Is there an elegant solution?
Thx!
You can split the strings with strsplit.
Split the strings in the second column:
splitted <- strsplit(as.character(df$v2), ";")
Create a new data frame:
data.frame(v1 = rep.int(df$v1, sapply(splitted, length)), v2 = unlist(splitted))
The result:
v1 v2
1 1 a
2 1 b
3 1 c
4 2 d
5 2 e
6 2 f

Resources