How to create an empty column - r

I have to consider three columns of a dataset.
One of them has values from 1 to 10, while the others have values from 2 to 10. I wanted to sum the frequencies for each value for all the three columns but an error appears, I think because two columns don't have values for 1.
How can I solve it?
This is what I have:
take.care
face_prod 2 3 4 5 6 7 8 9 10
anti-age 0 1 0 5 3 8 4 1 3
Hydrating 2 3 1 8 9 14 9 3 9
normal skin 0 0 0 0 4 0 1 0 1
Other 0 1 0 1 1 0 0 0 0
purifying 0 0 1 1 4 7 8 4 5
sensitive skin 0 0 0 0 1 2 0 0 1
look.fundam
face_prod 2 3 4 5 6 7 8 9 10
anti-age 0 0 0 2 2 4 3 5 9
Hydrating 1 0 1 4 12 7 10 5 18
normal skin 0 0 0 0 1 2 1 1 1
Other 0 1 0 0 2 0 0 0 0
purifying 0 1 0 0 3 5 9 3 9
sensitive skin 0 1 0 0 0 0 1 1 1
good.app
face_prod 1 2 3 4 5 6 7 8 9 10
anti-age 0 1 1 3 5 2 3 6 1 3
Hydrating 4 1 5 5 8 9 10 7 4 5
normal skin 0 0 0 0 2 2 1 0 0 1
Other 2 0 1 0 0 0 0 0 0 0
purifying 2 0 1 2 3 4 7 5 5 1
sensitive skin 1 0 0 0 2 0 0 0 0 1
It's not a dataset but the result of the table() function

If there are some levels missing, an option is to standardize with factor and levels specified as 1 to 10
nm1 <- c('take.care', 'look.fundam', 'good.app')
df1[nm1] <- lapply(df1[nm1], factor, levels = 1:10)
and now use the table

Related

How can I calculate the percentage score from test results using tidyverse?

Rather than calculate each individuals score, I want to calculate the percentage of individuals who answered the question correctly. Below is the tibble containing the data, the columns are the candidates, a-r, and the rows are the questions. The data points are the answers given, and the column on the right, named 'correct', shows the correct answer.
A tibble: 20 x 19
question a b c d e g h i j k l m n o p q r correct
<chr> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct>
1 001 3 3 3 0 4 0 1 4 4 0 2 3 2 0 3 0 3 1
2 002 2 4 2 3 4 NA 4 2 2 2 4 2 4 3 2 2 3 2
3 003 2 2 2 3 4 2 2 4 4 1 4 3 3 2 4 1 3 2
4 005 2 3 1 3 4 NA 2 4 4 2 4 1 4 2 4 2 2 2
5 006 3 1 2 3 3 NA 2 3 4 2 3 3 3 3 3 NA 3 3
6 008 3 3 3 3 3 1 1 3 3 1 3 3 3 3 3 1 3 3
7 010 4 5 4 3 4 4 4 4 4 3 4 4 5 4 4 3 4 4
8 011 3 3 5 3 3 3 3 3 5 4 5 4 4 3 3 2 5 5
9 013 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0
10 014 0 0 0 2 0 1 0 0 0 0 2 0 2 0 0 0 0 0
11 016 3 3 0 0 4 1 1 4 4 2 3 3 3 3 1 0 3 0
12 017 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
13 019 0 1 0 2 1 1 0 1 0 1 2 2 2 1 0 1 1 0
14 020 0 0 0 0 0 0 0 0 0 0 1 3 0 0 0 0 0 0
15 039 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0
16 041 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
17 045 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
18 047 0 0 0 0 0 NA 0 0 0 0 1 0 0 0 0 0 0 0
19 049 3 3 3 3 4 NA 2 4 x 2 4 3 5 3 1 1 3 3
20 050 0 3 3 0 1 NA 0 3 3 0 x 0 0 0 0 0 3 1
I would like to generate a column 'percentage' that gives the proportion of correct answers for each question. I suspect I have to do loops or row-wise operations, but I'm so far out of my depth with that, I just can't figure out how to compare factors. I've tried mutate(), if_else(), group_by() and much more but have not managed to get close to an answer.
Any help would be greatly appreciated.
If your data.frame is called data you may try
library(dplyr)
data %>% rowwise() %>%
mutate(percentage = sum(c_across(a:r) == correct) / length(c_across(a:r)))
You can try this solution using a loop:
#Code
#First select the range of individuals a to r
index <- 2:18
#Create empty var to save results
df$Count <- NA
df$Prop <- NA
#Apply function
for(i in 1:dim(df)[1])
{
x <- df[i,index]
count <- length(which(x==df$correct[i]))
percentage <- count/dim(x)[2]
#Assign
df$Count[i] <- count
df$Prop[i] <- percentage
}
Output:
question a b c d e g h i j k l m n o p q r correct Count Prop
1 1 3 3 3 0 4 0 1 4 4 0 2 3 2 0 3 0 3 1 1 0.05882353
2 2 2 4 2 3 4 NA 4 2 2 2 4 2 4 3 2 2 3 2 8 0.47058824
3 3 2 2 2 3 4 2 2 4 4 1 4 3 3 2 4 1 3 2 6 0.35294118
4 5 2 3 1 3 4 NA 2 4 4 2 4 1 4 2 4 2 2 2 6 0.35294118
5 6 3 1 2 3 3 NA 2 3 4 2 3 3 3 3 3 NA 3 3 10 0.58823529
6 8 3 3 3 3 3 1 1 3 3 1 3 3 3 3 3 1 3 3 13 0.76470588
7 10 4 5 4 3 4 4 4 4 4 3 4 4 5 4 4 3 4 4 12 0.70588235
8 11 3 3 5 3 3 3 3 3 5 4 5 4 4 3 3 2 5 5 4 0.23529412
9 13 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 14 0.82352941
10 14 0 0 0 2 0 1 0 0 0 0 2 0 2 0 0 0 0 0 13 0.76470588
11 16 3 3 0 0 4 1 1 4 4 2 3 3 3 3 1 0 3 0 3 0.17647059
12 17 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 15 0.88235294
13 19 0 1 0 2 1 1 0 1 0 1 2 2 2 1 0 1 1 0 5 0.29411765
14 20 0 0 0 0 0 0 0 0 0 0 1 3 0 0 0 0 0 0 15 0.88235294
15 39 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 14 0.82352941
16 41 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 14 0.82352941
17 45 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 1.00000000
18 47 0 0 0 0 0 NA 0 0 0 0 1 0 0 0 0 0 0 0 15 0.88235294
19 49 3 3 3 3 4 NA 2 4 NA 2 4 3 5 3 1 1 3 3 7 0.41176471
20 50 0 3 3 0 1 NA 0 3 3 0 NA 0 0 0 0 0 3 1 1 0.05882353
You had some x in answers so I have replaced by NA in order to make the loop works.

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?

How to remove a row in a contingency table in R?

Let say I have a contingency table (made using the table function in R).
digit
ID 1 2 3 4 5 6 7 8 9
1672120 23 16 8 10 12 13 3 3 5
1672121 2 1 0 0 0 0 1 0 0
1672122 1 2 1 0 1 0 0 1 0
1672123 0 1 1 0 0 0 0 0 0
1672124 1 1 0 1 1 0 0 0 0
1672125 5 2 5 1 1 1 0 0 2
1672127 2 1 2 1 0 0 0 0 0
1672128 2 0 0 1 0 1 0 0 1
1672129 1 0 1 0 0 0 1 0 0
If I want to remove the rows where the number of counts is smaller than 5 from the contingency table, how should I do it?
Since you don't provide reproducible sample data here is an example based on the mtcars dataset
Let's create a count table of mtcars$gear vs. mtcars$carb
tbl <- table(mtcars$gear, mtcars$carb)
#
# 1 2 3 4 6 8
# 3 3 4 3 5 0 0
# 4 4 4 0 4 0 0
# 5 0 2 0 1 1 1
We then select only those rows where at least one count is larger than 2
tbl[apply(tbl > 2, 1, any), ]
#
# 1 2 3 4 6 8
# 3 3 4 3 5 0 0
# 4 4 4 0 4 0 0

How to create new columns in R every time a given value appears?

I have a question regarding creating new columns if a certain value appears in an existing row.
N=5
T=5
time<-rep(1:T, times=N)
id<- rep(1:N,each=T)
dummy<- c(0,0,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,1,0)
df <- data.frame(id, time, dummy)
id time dummy
1 1 1 0
2 1 2 0
3 1 3 1
4 1 4 1
5 1 5 0
6 2 1 0
7 2 2 0
8 2 3 1
9 2 4 0
10 2 5 0
11 3 1 0
12 3 2 1
13 3 3 0
14 3 4 1
15 3 5 0
16 4 1 0
17 4 2 0
18 4 3 0
19 4 4 0
20 4 5 0
21 5 1 1
22 5 2 0
23 5 3 0
24 5 4 1
25 5 5 0
In this case we have some cross-sections in which more than one 1 appears. Now I try to create a new dummy variable/column for each additional 1. After that, for each dummy, the rows for each cross-section should also be filled with a 1 after the first 1 appears. I can fill the rows by using group_by(id) and the cummax function on each column. But how do I get new variables without going through every cross-section manually? So I want to achieve the following:
id time dummy dummy2
1 1 1 0 0
2 1 2 0 0
3 1 3 1 0
4 1 4 1 1
5 1 5 1 1
6 2 1 0 0
7 2 2 0 0
8 2 3 1 0
9 2 4 1 0
10 2 5 1 0
11 3 1 0 0
12 3 2 1 0
13 3 3 1 0
14 3 4 1 1
15 3 5 1 1
16 4 1 0 0
17 4 2 0 0
18 4 3 0 0
19 4 4 0 0
20 4 5 0 0
21 5 1 1 0
22 5 2 1 0
23 5 3 1 0
24 5 4 1 1
25 5 5 1 1
Thanks! :)
You can use cummax and you would need cumsum to create dummy2
df %>%
group_by(id) %>%
mutate(dummy1 = cummax(dummy), # don't alter 'dummy' here we need it in the next line
dummy2 = cummax(cumsum(dummy) == 2)) %>%
as.data.frame() # needed only to display the entire result
# id time dummy dummy1 dummy2
#1 1 1 0 0 0
#2 1 2 0 0 0
#3 1 3 1 1 0
#4 1 4 1 1 1
#5 1 5 0 1 1
#6 2 1 0 0 0
#7 2 2 0 0 0
#8 2 3 1 1 0
#9 2 4 0 1 0
#10 2 5 0 1 0
#11 3 1 0 0 0
#12 3 2 1 1 0
#13 3 3 0 1 0
#14 3 4 1 1 1
#15 3 5 0 1 1
#16 4 1 0 0 0
#17 4 2 0 0 0
#18 4 3 0 0 0
#19 4 4 0 0 0
#20 4 5 0 0 0
#21 5 1 1 1 0
#22 5 2 0 1 0
#23 5 3 0 1 0
#24 5 4 1 1 1
#25 5 5 0 1 1

How to get a square table?

I've got the following code to create a classification table in R:
> table(class = class1, truth = valid[,1])
1 2 3 4 5 6 7 8 9 10 11 12
1 357 73 0 0 47 0 5 32 20 0 4 7
2 25 71 0 0 23 4 1 0 2 1 8 3
3 1 2 120 1 5 0 1 0 0 0 0 0
4 0 0 0 77 0 0 0 0 1 0 0 0
5 15 27 0 0 67 6 7 0 4 1 5 7
6 1 2 0 0 2 44 0 0 0 7 7 0
7 1 1 0 0 10 0 66 0 1 0 1 7
9 1 0 0 0 3 0 0 2 8 0 0 2
10 1 1 0 0 1 6 0 0 0 17 0 0
11 0 7 0 0 3 1 0 0 0 4 10 2
12 0 1 0 0 1 0 0 0 0 0 0 1
However, I need this table to be a square (line 8 is missing in this example), i.e. the number of rows should equal the number of columns, and I need the rownames and colnames to be preserved. The missing line should be filled with zeros. Any way of doing this?
The problem most probably comes from a difference in levels.
Try copying the levels from valid to class1:
class1 <- factor(class1, levels=levels(valid[,1])
table(class = class1, truth = valid[,1])

Resources