How to get the first occurance of a character in R - r

I have a dataframe with repeated IDs, I want to get a colunm that contains the very first occurance of each ID.
For instance:
ID <- as.character( "ae61_10", "ae61_10", "ae61_10", "ae61_10", "ae61_10", "ae61_1", "ae61_1", "ae61_1", "ae61_11", "ae61_2", "ae61_2", "ae61_3", "ae61_4", "ae61_4", "ae61_5", "ae61_6", "ae61_7", "ae61_8", "ae61_8", "ae61_8", "ae61_9")
df <- data.frame(ID)
df
ID
1 ae61_10
2 ae61_10
3 ae61_10
4 ae61_10
5 ae61_10
6 ae61_1
7 ae61_1
8 ae61_1
9 ae61_11
10 ae61_2
11 ae61_2
12 ae61_3
13 ae61_4
14 ae61_4
15 ae61_5
16 ae61_6
17 ae61_7
18 ae61_8
19 ae61_8
20 ae61_8
21 ae61_9
I want to get the colunm FIRST_OC, which returns 1 if that is the first ID occurance, and 0 if it is not. Like this:
ID FIRST_OC
1 ae61_10 1
2 ae61_10 0
3 ae61_10 0
4 ae61_10 0
5 ae61_10 0
6 ae61_1 1
7 ae61_1 0
8 ae61_1 0
9 ae61_11 1
10 ae61_2 1
11 ae61_2 0
12 ae61_3 1
13 ae61_4 1
14 ae61_4 0
15 ae61_5 1
16 ae61_6 1
17 ae61_7 1
18 ae61_8 1
19 ae61_8 0
20 ae61_8 0
21 ae61_9 1
I have tried this command, but it did not work:
df$FIRST_OC <- 0
FIRST_OC <- df[match(unique(df$ID), df$ID),]
df$FIRST_OC[which(df$ID %in% FIRST_OC)] <- 1
Could somebody help me to figure out what is going wrong?
Many thanks.

duplicated is the function to use!
df$FIRST_OC <- ifelse(!duplicated(df$ID), 1, 0)
# OR
df$FIRST_OC <- as.numeric(!duplicated(df$ID))

We can use duplicated
+(!duplicated(df$ID))
#[1] 1 0 0 0 0 1 0 0 1 1 0 1 1 0 1 1 1 1 0 0 1

Related

group data which are either 0 or 1 [duplicate]

This question already has answers here:
Create counter of consecutive runs of a certain value
(4 answers)
Closed 1 year ago.
I have a vector Blinks whose values are either 0 or 1:
df <- data.frame(
Blinks = c(0,0,1,1,1,0,0,1,1,1,1,0,0,1,1)
)
I want to insert a grouping variable for when Blinks == 1. I'm using rleidfor this but the grouping seems to count in the instances where Blinks == 0:
library(dplyr)
library(data.table)
df %>%
mutate(Blinks_grp = ifelse(Blinks > 0, rleid(Blinks), Blinks))
Blinks Blinks_grp
1 0 0
2 0 0
3 1 2
4 1 2
5 1 2
6 0 0
7 0 0
8 1 4
9 1 4
10 1 4
11 1 4
12 0 0
13 0 0
14 1 6
15 1 6
How can I obtain the correct result:
1 0 0
2 0 0
3 1 1
4 1 1
5 1 1
6 0 0
7 0 0
8 1 2
9 1 2
10 1 2
11 1 2
12 0 0
13 0 0
14 1 3
15 1 3
One option could be:
df %>%
mutate(Blinks_grp = with(rle(Blinks), rep(cumsum(values) * values, lengths)))
Blinks Blinks_grp
1 0 0
2 0 0
3 1 1
4 1 1
5 1 1
6 0 0
7 0 0
8 1 2
9 1 2
10 1 2
11 1 2
12 0 0
13 0 0
14 1 3
15 1 3

formatting table/matrix in R

I am trying to use a package where the table they've used is in a certain format, I am very new to R and don't know how to get my data in this same format to be able to use the package.
Their table looks like this:
Recipient
Actor 1 10 11 12 2 3 4 5 6 7 8 9
1 0 0 0 1 3 1 1 2 3 0 2 6
10 1 0 0 1 0 0 0 0 0 0 0 0
11 13 5 0 5 3 8 0 1 3 2 2 9
12 0 0 2 0 1 1 1 3 1 1 3 0
2 0 0 2 0 0 1 0 0 0 2 2 1
3 9 9 0 5 16 0 2 8 21 45 13 6
4 21 28 64 22 40 79 0 16 53 76 43 38
5 2 0 0 0 0 0 1 0 3 0 0 1
6 11 22 4 21 13 9 2 3 0 4 39 8
7 5 32 11 9 16 1 0 4 33 0 17 22
8 4 0 2 0 1 11 0 0 0 1 0 1
9 0 0 3 1 0 0 1 0 0 0 0 0
Where mine at the moment is:
X0 X1 X2 X3 X4 X5
0 0 2 3 3 0 0
1 1 0 4 2 0 0
2 0 0 0 0 0 0
3 0 2 2 0 1 0
4 0 0 3 2 0 2
5 0 0 3 3 1 0
I would like to add the recipient and actor to mine, as well as change to row and column names to 1, ..., 6.
Also my data is listed under Data in my Workspace and it says:
'num' [1:6,1:6] 0 1 ...
Whereas the example data in the workspace is shown in Values as:
'table' num [1:12,1:12] 0 1 13 ...
Please let me know if you have suggestion to get my data in the same type and style as theirs, all help is greatly appreciated!
OK, so you have a matrix like so:
m <- matrix(c(1:9), 3)
rownames(m) <- 0:2
colnames(m) <- paste0("X", 0:2)
# X0 X1 X2
#0 1 4 7
#1 2 5 8
#2 3 6 9
First you need to remove the Xs and turn it into a table:
colnames(m) <- sub("X", "", colnames(m))
m <- as.table(m)
# 0 1 2
#0 1 4 7
#1 2 5 8
#2 3 6 9
Then you can set the dimension names:
names(dimnames(m)) <- c("Actor", "Recipient")
# Recipient
#Actor 0 1 2
# 0 1 4 7
# 1 2 5 8
# 2 3 6 9
However, usually you would create the contingency table from raw data using the table function, which would automatically return a table object. So, maybe you should fix the step creating your matrix?

Distributing from frequency table to original data?

I have a data_aa as below
ID x.cal tx.cal
1 0 0
2 0 0
3 0 0
4 0 0
5 1 1
6 10 1
7 10 1
8 11 1
9 11 1
With above data_aa, I made a frequency table as below and got an result_A variable.
x.cal tx.cal frequency result_A
0 0 3223 0.05268579
1 1 35 0.05048418
1 10 2 0.89475308
1 11 1 0.98251303
1 12 1 1.06831347
1 13 1 1.15179768
I want to add result_A value to my original data data_aa.
How I can redistribute result_A from frequency data to original data_aa ?
I want to add result_A value to each individual (ID) from frequency table.
My desired table as below
ID x.cal tx.cal result_A
1 0 0 0.05268579
2 0 0 0.05268579
3 0 0 0.05268579
4 0 0 0.05268579
5 1 1 0.05048418
6 10 1 0.89475308
7 10 1 0.89475308
8 11 1 0.98251303
9 11 1 0.98251303
Use David's approach to combine the dataframes, then clean the resultant to get the format you want.
df <- merge(data_aa, freq_aa, by = c("x.cal", "tx.cal"), all.x = T)
result <- df[c("ID", "x.cal", "tx.cal", "result_A")]

Removing the unordered pairs repeated twice in a file in R

I have a file like this in R.
**0 1**
0 2
**0 3**
0 4
0 5
0 6
0 7
0 8
0 9
0 10
**1 0**
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
**3 0**
As we can see, there are similar unordered pairs in this ( marked pairs ), like,
1 0
and
0 1
I wish to remove these pairs. And I want to count the number of such pairs that I have and append the count in front of the tow that is repeated. If not repeated, then 1 should be written in the third column.
For example ( A sample of the output file )
0 1 2
0 2 1
0 3 2
0 4 1
0 5 1
0 6 1
0 7 1
0 8 1
0 9 1
0 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
How can I achieve it in R?
Here is a way using transform, pmin and pmax to reorder the data by row, and then aggregate to provide a count:
# data
x <- data.frame(a=c(rep(0,10),rep(1,10),3),b=c(1:10,0,11:19,0))
#logic
aggregate(count~a+b,transform(x,a=pmin(a,b), b=pmax(a,b), count=1),sum)
a b count
1 0 1 2
2 0 2 1
3 0 3 2
4 0 4 1
5 0 5 1
6 0 6 1
7 0 7 1
8 0 8 1
9 0 9 1
10 0 10 1
11 1 11 1
12 1 12 1
13 1 13 1
14 1 14 1
15 1 15 1
16 1 16 1
17 1 17 1
18 1 18 1
19 1 19 1
Here's one approach:
First, create a vector of the columns sorted and then pasted together.
x <- apply(mydf, 1, function(x) paste(sort(x), collapse = " "))
Then, use ave to create the counts you are looking for.
mydf$count <- ave(x, x, FUN = length)
Finally, you can use the "x" vector again, this time to detect and remove duplicated values.
mydf[!duplicated(x), ]
# V1 V2 count
# 1 0 1 2
# 2 0 2 1
# 3 0 3 2
# 4 0 4 1
# 5 0 5 1
# 6 0 6 1
# 7 0 7 1
# 8 0 8 1
# 9 0 9 1
# 10 0 10 1
# 12 1 11 1
# 13 1 12 1
# 14 1 13 1
# 15 1 14 1
# 16 1 15 1
# 17 1 16 1
# 18 1 17 1
# 19 1 18 1
# 20 1 19 1

cumulative counter in dataframe R

I have a dataframe with many rows, but the structure looks like this:
year factor
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 1
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 1
18 0
19 0
20 0
I would need to add a counter as a third column. It should count the cumulative cells that contains zero until it set again to zero once the value 1 is encountered. The result should look like this:
year factor count
1 0 0
2 0 1
3 0 2
4 0 3
5 0 4
6 0 5
7 0 6
8 0 7
9 1 0
10 0 1
11 0 2
12 0 3
13 0 4
14 0 5
15 0 6
16 0 7
17 1 0
18 0 1
19 0 2
20 0 3
I would be glad to do it in a quick way, avoiding loops, since I have to do the operations for hundreds of files.
You can copy my dataframe, pasting the dataframe in "..." here:
dt <- read.table( text="...", , header = TRUE )
Perhaps a solution like this with ave would work for you:
A <- cumsum(dt$factor)
ave(A, A, FUN = seq_along) - 1
# [1] 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3
Original answer:
(Missed that the first value was supposed to be "0". Oops.)
x <- rle(dt$factor == 1)
y <- sequence(x$lengths)
y[dt$factor == 1] <- 0
y
# [1] 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 2 3

Resources