Averages based on elements from other columns - r

So, I’ve been trying to get this working but for some reason, I’m just not making any progress on this. And I was hoping if you guys could help me. Pretty much, I have a data frame that I would like to get the average of a specific range of values, where these values are from other columns within the same data frame, for each user.
So, let’s say I have this data frame.
a<-data.frame(user=c(rep(1,10),rep(2,10),rep(3,10)),
values=c(1:30),toot=c(rep(4,10),rep(5,10),rep(3,10)))
user values toot
1 1 4
1 2 4
1 3 4
1 4 4
1 5 4
1 6 4
1 7 4
1 8 4
1 9 4
1 10 4
2 11 5
2 12 5
2 13 5
2 14 5
2 15 5
2 16 5
2 17 5
2 18 5
2 19 5
2 20 5
3 21 3
3 22 3
3 23 3
3 24 3
3 25 3
3 26 3
3 27 3
3 28 3
3 29 3
3 30 3
So, what I would like is to take the average of the values between 2 elements prior of the toot element through the toot element.
Here's what I'm looking for:
user values toot deck
1 1 4 3
1 2 4 3
1 3 4 3
1 4 4 3
1 5 4 3
1 6 4 3
1 7 4 3
1 8 4 3
1 9 4 3
1 10 4 3
2 11 5 14
2 12 5 14
2 13 5 14
2 14 5 14
2 15 5 14
2 16 5 14
2 17 5 14
2 18 5 14
2 19 5 14
2 20 5 14
3 21 3 22
3 22 3 22
3 23 3 22
3 24 3 22
3 25 3 22
3 26 3 22
3 27 3 22
3 28 3 22
3 29 3 22
3 30 3 22
As you see, for user 1, that user’s toot value is 4, so I want to take the average of user’s 1 values at the 4th element and average it with the 2 elements before it.
This is what I have so far (with many variations of this and with the by function):
a$deck<-ave(a$values,a$user,FUN=function(x)
{
z<-a$toot
y<-z-2
mean(x[y:z])
})
But the problem is that it’s not using the toot value as it’s starting position. Here are the warning messages:
> Warning messages:
1: In y:z : numerical expression has 30 elements: only the first used
2: In y:z : numerical expression has 30 elements: only the first used
Error in mean(x[y:z]) :
error in evaluating the argument 'x' in selecting a method for function 'mean': Error in x[y:z] : only 0's may be mixed with negative subscripts
Anything is welcomed and appreciated, thanks.

You can do it with by(). Like:
do.call(rbind, by(a, a$user, function(x) { cbind(x,deck=mean(x$values[x$toot[1]:(x$toot[1]-2)])) }))

library(plyr)
ddply(a,.(user),function(df) {
df$deck <- mean(df$values[(df$toot[1]-2):df$toot[1]])
df
})

Related

Assign ID based on a sequence of consecutive days in R

I have a dataset with repeated measures which I want to use to assign IDs. The repeated measures are from a sequence of consecutive days. However, the sequence itself may be unbalanced (e.g., some have more days while others have less, some start with day 1 but a few others may start with 2 or 3). My question is how to create and assign the same ID withinid the same block of sequence. Here is a toy dataset:
days <- data.frame(
day = c(1L,2L,3L,4L,5L,6L,8L,9L,10L,
2L,3L,4L,5L,6L,7L,9L,10L,
1L,2L,4L,5L,6L,8L,9L,10L,
1L,2L,3L,4L,5L,6L,7L,8L,9L,10L)
)
Here is the end result I expect:
id day
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
6 1 6
7 1 8
8 1 9
9 1 10
10 2 2
11 2 3
12 2 4
13 2 5
14 2 6
15 2 7
16 2 9
17 2 10
18 3 1
19 3 2
20 3 4
21 3 5
22 3 6
23 3 8
24 3 9
25 3 10
26 4 1
27 4 2
28 4 3
29 4 4
30 4 5
31 4 6
32 4 7
33 4 8
34 4 9
35 4 10
Get the difference between adjacent elements and check if it is less than 0, take the cumulative sum
days$id <- cumsum(c(TRUE, diff(days$day) < 0))

Vectorized Conditional Random Matching

I want to create conditional random pairs without using for-loops so I can use the code with large datasets. At first, I create rows with unique IDs and randomly assign two different "types" to my rows:
df<-data.frame(id=1:10,type=NA,partner=NA)
df[sample(df$id,nrow(df)/2),"type"]<-1 ##random 50% type 1
df[which(is.na(df$type)==TRUE),"type"]<-2 ##other 50% type 2
df
id type partner
1 1 2 NA
2 2 1 NA
3 3 1 NA
4 4 1 NA
5 5 2 NA
6 6 1 NA
7 7 1 NA
8 8 2 NA
9 9 2 NA
10 10 2 NA
Now I want them to receive a random partner of the opposite type. So I randomize my type 1 IDs and match them to some type 2 IDs like so:
df$partner[which(df$type==2)]<-sample(df$id[which(df$type==1)],
nrow(df)/2)
df
id type partner
1 1 2 4
2 2 1 NA
3 3 1 NA
4 4 1 NA
5 5 2 2
6 6 1 NA
7 7 1 NA
8 8 2 6
9 9 2 3
10 10 2 7
And that's where I'm stuck. For some reason I can't think of a vectorized way to tell R "take the IDs of type 1, look where these IDs are in df$partner and return the corresponding row ID as df$partner instead of NA".
One example for a for-loop for conditional random pairing can be found here: click
I'm pretty sure that that's very basic and doable, however, any help appreciated!
Presumably, you want the type 1 and type 2 matched together to have each other's id in their respective partner entries. Fully vectorized solution.
# Define number of ids
n = 100
# Generate startingn data frame
df = data.frame(id = 1:n, type = NA, partner = NA)
# Generate the type column
df$type[(a<-sample(df$id, n/2))] = 1
df$type[(b<-setdiff(1:100, a))] = 2
# Select a random partner id from the other type
df$partner[a] = sample(df$id[b])
# Fill in partner values based on previous line
df$partner[b] = df$id[match(df$id[b], df$partner)]
Output:
id type partner
1 2 11
2 1 13
3 2 19
4 2 10
5 1 17
6 2 28
7 2 27
8 2 21
9 1 22
10 1 4
11 1 1
12 2 20
13 2 2
14 2 25
15 2 24
16 2 30
17 2 5
18 2 29
19 1 3
20 1 12
21 1 8
22 2 9
23 2 26
24 1 15
25 1 14
26 1 23
27 1 7
28 1 6
29 1 18
30 1 16

Subset data frame based on column values

I have a data frame consisting of the fluorescence read out of multiple cells tracked over time, for example:
Number=c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
Fluorescence=c(9,10,20,30,8,11,21,31,6,12,22,32,7,13,23,33)
df = data.frame(Number, Fluorescence)
Which gets:
Number Fluorescence
1 1 9
2 2 10
3 3 20
4 4 30
5 1 8
6 2 11
7 3 21
8 4 31
9 1 6
10 2 12
11 3 22
12 4 32
13 1 7
14 2 13
15 3 23
16 4 33
Number pertains to the cell number. What I want is to collate the fluorescence readout based on the cell number. The data.frame here has it counting 1-4, whereas really I want something like this:
Number Fluorescence
1 1 9
2 1 8
3 1 6
4 1 7
5 2 10
6 2 11
7 2 12
8 2 13
9 3 20
10 3 21
11 3 22
12 3 23
13 4 30
14 4 31
15 4 32
16 4 33
Or even more ideal would be having columns based on Number, then respective cell fluorescence:
1 2 3 4
1 9 10 20 30
2 8 11 21 31
3 6 12 22 32
4 7 13 23 33
I've used the which function to extract them one at a time:
Cell1=df[which(df[,1]==1),2]
But this would require me to write a line for each cell (of which there are hundreds).
Thank you for any help with this! Apologies that I'm still a bit of an R noob.
How about this:
library(tidyr);library(data.table)
number <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
fl <- c(9,10,20,30,8,11,21,31,6,12,22,32,7,13,23,33)
df <- data.table(number,fl)
df[, index:=1:.N, keyby=number]
df
number fl index
1: 1 9 1
2: 1 8 2
3: 1 6 3
4: 1 7 4
5: 2 10 1
6: 2 11 2
7: 2 12 3
8: 2 13 4
9: 3 20 1
10: 3 21 2
11: 3 22 3
12: 3 23 4
13: 4 30 1
14: 4 31 2
15: 4 32 3
16: 4 33 4
The index is added for the unique identifier in spread function from tidyr. Look this post for more information.
spread(df,number,fl)
index 1 2 3 4
1: 1 9 10 20 30
2: 2 8 11 21 31
3: 3 6 12 22 32
4: 4 7 13 23 33

R which argument fits well to obtain nonuniform bins using "plot" to build an informative histogram

I am new to R,I am trying to plot a cumulative frequency histogram(non-uniform bins) for a huge amount of data(few millions of positive numbers with a minimum value "1" and maximum value varies from data to data like for instance 1*10^6 or 1*10^5).I used this simple code to generate a histogram with the data.
for example:-sample data
[89601] 10 2 2 4 3 12 3 25 25 2
[89611] 5 5 5 2 23 22 14 8 13 10
[89621] 13 19 157 2 3 2 4 2 3 33
[89631] 22 2 14 9 2 3 3 3 8 2
[89641] 8 3 2 127 8 2 18 2 4 2
[89651] 2 13 3 34 8 2 6 10 3 7
[89661] 3 9 7 3 36 9 5 2 10 15
[89671] 7 2 23 2 2 2 2 7 6 25
[89681] 3 3 2 6 37 49 28 11 3 35
[89691] 2 2 8 3 3 2 2 4 3 12
[89701] 3 5 2 7 3 2 15 6 3 14
[89711] 13 5 3 2 2 8 34 4 4 65
[89721] 5 9 12 2 11 2 2 79 9 13
[89731] 2 66 2 9 10 22 11 2 6 3
[89741] 12 2 11 5 4 4 2 4 3 4
[89751] 2 8 9 3 2 2 84 7 11 10
[89761] 8 30 16 3 63 2 2 24 13 2
[89771] 11 37 2 9 21 21 10 2 2 49
[89781] 3 3 8 5 2 19 9 6 5 4
[89791] 4 2 9 2 10 33 5 4 2 2
[89801] 4 2 2 4 9 3 11 2 5 142
[89811] 17 2 11 4 2 8 26 2 9 8
[89821] 10 2 4 2 5 2 20 7 145 11
[89831] 22 19 8 14 18 39 3 2 3 3
[89841] 2 11 10 3 2 3 3 5 6 12
[89851] 17 5 3 8 2 2 2 2 2 5
[89861] 4 2 13 3 2 2 2 2 3 2
[89871] 4 3 21 2 6 2 8 9 7 14
[89881] 2 582 3 15 11 3 20 16 9 8
[89891] 6 2 6 7 3 20 17 2 9 5
[89901] 5 11 2 12 7 2 46 2 144 9
[89911] 2 3 36 25 3 2 16 2 2 119
[89921] 5 5 10 6 2 2 6 84 13 2
[89931] 2 6 6 2 17 3 7 4 102 48
data <- read.table("sample.txt", header=FALSE)
data <- hist(data$V1, breaks=length(data$V1), xlim=c(0,4000000))
plot(data)
when I did this I could get a histogram with all the data(positive numbers)on x axis and counts on y-axis.Then again I changed the limit of the x only upto the area of interest
plot(data, xlim=c(0,200000))
Like before a histogram is plotted,but using "plot" I couldn't define the number of bins and hence the histogram is not clear(not like bars which I want to be) and informative.
As I am new to this forum,I have no idea how to upload images,so I couldn't provide with the histogram.
Any suggestions would be very helpful.
For plotting histogram you can use hist() function just this way:
hist(data$V1, xlim=c(0,200000), breaks=100)
The breaks parameter shows, how many bars will be plotted. But this number is related to all plot, not to xlim you specified. So, at first it will make a histogram with given number of breakes and after that it will cut the part of plot you need.
But there is another way to plot the bars:
data <- read.table("sample.txt", header=FALSE)
data.hist <- hist(data$V1, breaks=length(data$V1), xlim=c(0,4000000))
plot(data.hist$counts, type='h')
The hist function returns an object which represents histogram parameters.
I assume, you are interested in "counts" field.
You can plot this info in histogram-like way by defining type='h'.

How to create a dataframe with different number of values?

When I create a dataframe I do:
dt = data.frame(a=c(1:5),b=c(1:20))
dt
a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 1 6
7 2 7
8 3 8
9 4 9
10 5 10
11 1 11
12 2 12
13 3 13
14 4 14
15 5 15
16 1 16
17 2 17
18 3 18
19 4 19
20 5 20
as you can see the value of the first column (a) are repeated.
How can I create different "columns" with different number of values?
Thanks
H
Use a list. A data.frame is a special kind of list in which all elements are of the same length.
list(a=c(1:5),b=c(1:20))
$a
[1] 1 2 3 4 5
$b
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Resources