hello everyone i need help on how to make matrices of proportions.

Using the below data set
Kindly help with the codes and how to

question matrix, which is the proportion of question to which the respondent gave a YES out of the 5 total question

Question 1 to 5 QS1CD QS2CD QS3CD QS4CD QS5CD Question contribution weight (1=GOOD ANSWER, 2=BAD QScw1CD QScw2CD QScw3CD QScw4CD QScw5CD
1 3 2 NA 2 2 1 2 2 1
3 3 2 NA 2 1 1 2 2 1
1 3 2 NA 2 2 1 2 2 1
2 3 2 NA 2 2 1 2 2 1
2 5 2 NA 2 2 2 2 2 1
3 3 1 1 2 1 1 1 2 1
6 3 1 1 2 2 1 1 2 1
6 5 1 1 5 2 2 1 2 2
1 5 1 1 2 2 2 1 2 1

do you mean something like this ?

library(tidyverse)

(df <- tribble (~A,~B,~Aw , ~Bw,
               0,1,2,3,
               1,1,.5,1))

(raw_df <- select(df,1:2))
(wgt_df <- select(df,3:4))

(product_df <- map2_dfc(.x=raw_df,
                        .y=wgt_df,
                        .f=~.x*.y))

(total_val <- sum(product_df))
(prop_df <- product_df/max_val)

Thank you.
But "NO", I want a column: Question_metrics for 1 to 10 respondents.
using question 1 to 5 and 'yes' i.e 1, contribution weight

for your example what would that be ?
can you calculate it 'by hand' for the first respondent for example ?

Not really.
Question$Question_Metric <- apply(Question, 1, function(x)
length(which(x == "YES" | x == "YES")) / 5)

where Question is the name of data set of question 1 to 5
Also i don't understand or the meaning of this code.

the code you shared seems to be about counting Yes's and then normalising by 5.

lets break this down and make this as simple as we can.
Do you have initial data that you are starting with?
What does a sample of that look like ?
What operation do you want to perform on it ?

i have this data for about four variables having different types of questions. i want to make metric of each variables to do liner regression, corelationship and PCA

This one variables with five questions.

hello. do understand my need?

sorry no.

I encourage you to read this guide to how to make/share an example
FAQ: How to do a minimal reproducible example ( reprex ) for beginners

QS1	QS2	QS3	QS4	QS5	QHS10	QHS11	QHS12	QHS13	QHS14	QHS15	QScw1+G14:Q23	QScw2	QScw3	QScw4	QScw5	QHScw10	QHScw11	QHScw12	QHScw13	QHScw14	QHScw15
1	3	2	NA	2	2	2	2	2	2	2	2	1	2	2	1	2	2	2	2	2	2
3	3	2	NA	2	2	2	2	2	2	2	1	1	2	2	1	2	2	2	2	2	2
1	3	2	NA	2	2	2	2	1	2	3	2	1	2	2	1	2	2	2	1	2	2
2	3	2	NA	2	2	2	2	1	2	3	2	1	2	2	1	2	2	2	1	2	2
2	5	2	NA	2	2	2	2	2	2	1	2	2	2	2	1	2	2	2	2	2	1
3	3	1	1	2	1	1	1	1	1	2	1	1	1	2	1	1	1	1	1	1	2
6	3	1	1	2	1	1	1	1	1	3	2	1	1	2	1	1	1	1	1	1	2
6	5	1	1	5	1	1	1	1	2	2	2	2	1	2	2	1	1	1	1	2	2
1	5	1	1	2	1	1	1	1	1	2	2	2	1	2	1	1	1	1	1	1	2

Question on sources (1 to 5) and Question on hygieneSanitation (11 to 15)
Question contribution weight, i.e QScw and QHSCW (1=GOOD ANSWER, 2=BAD)

codes

sources_QN<- QRE[,c(1:5),]
hygieneSanitation_QN<- QRE[,c(6:11),]

sources_QN1<- QRE[,c(12:16),]
hygieneSanitation_QN1<- QRE[,c(17:22),]

sources_QN$sources_Metric <- apply(sources_QN, 1, function(x)
length(which(x == "YES" | x == "YES")) / 5)
hygieneSanitation_QN$hygieneSanitation_Metric <- apply(hygieneSanitation_QN, 1, function(x)
length(which(x == "YES" | x == "YES")) / 6)

sources_QN$sources_Metric
[1] 0 0 0 0 0 0 0 0 0 0
same with hygieneSanitation_QN$hygieneSanitation_Metric given me zero. i dont want zero. what is the better way to get real vales other than zero?

i need the column for sources_Metric and hygieneSanitation_Metric, for further analysis, and they are both zeros what way can i get a better values and not zeros?

ok. you are on your way, but not really there yet.
I want you to understand that the purpose is to make it easy to help you. so you should provide things that people can take and look at on their computer without much effort.

If I tell you I have data like this ?

QS1 QS2 QS3
1 3 2
3 3 2

How would you put this into your RStudio to run operations on it ?
copy and paste it will give you error, right ?

so following the documentation I provided use dput() and provide a sample of your data that I can copy and paste and so use directly in my session.

structure(list(QS1 = c(1, 3), QS2 = c(3, 3), QS3 = c(2, 2)), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

This dput() output might look ugly but you can paste this into your session, and use it, and see the data.frame directly where it counts, in R studio!

Also, when writing code into forum posts, use a line of triple backticks to turn on or off code formatting to make your code look more readable. like this

```
code looks nice

this will give

code looks nice

check this
dput(QRE2)

structure(list(Age = c(27L, 45L, 19L, 17L, 32L, 35L, 27L, 23L,
20L, 30L, 51L, 31L, 31L, 64L, 48L, 22L, 16L, 20L, 19L, 35L),
SexCD = c(1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L), sources_Metric = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hygieneSanitation_Metric = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
attitudeWASH_Metric = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c("KI1", "KI2",
"KI3", "KI4", "KI5", "KI6", "KI7", "KI8", "KI9", "KI10", "KI11",
"KI12", "KI13", "KI14", "KI15", "KI16", "KI17", "KI18", "KI19",
"KI20"), class = "data.frame")

Edited with backticks to format as code

its not obvious how this relates to anything that you previously shared. it doesnt look anything like the example of your starting data that you pasted in table form twice earlier in this thread... has your starting point significantly changed while we've been talking ? or did you dput the 'wrong' table ?

Is same data, i was trying to take two variables out before.
hold on for few minutes let me re-arrange the date and codes

dput(sources_QN)
structure(list(QHS11CD = c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L
), QHS12CD = c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), QHS13CD = c(2L,
2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), QHS14CD = c(2L, 2L, 2L, 2L,
2L, 1L, 1L, 2L, 1L), QHS15CD = c(2L, 2L, 3L, 3L, 1L, 2L, 3L,
2L, 2L), sources_Metric = c(0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c("KI1",
"KI2", "KI3", "KI4", "KI5", "KI6", "KI7", "KI8", "KI9"), class = "data.frame")

sources_QN<- QRE10[,c(19:23),]
hygieneSanitation_QN<- QRE10[,c(24:29),]

sources_QN1<- QRE[,c(40:44),]
hygieneSanitation_QN1<- QRE[,c(45:50),]

sources_QN$sources_Metric <- apply(sources_QN, 1, function(x)
length(which(x == "YES" | x == "YES")) / 5)
hygieneSanitation_QN$hygieneSanitation_Metric <- apply(hygieneSanitation_QN, 1, function(x)
length(which(x == "YES" | x == "YES")) / 6)

here you will see sources_Metric to be zero. that is the issues....

your data as shared does not contain "YES" type entries so you shouldnt expect to detect any YES, 0 results would be expected....

I get the impression you might be trying to use someone elses code on your data, apologies in advance if I turn out to be mistaken about that. If thats correct however then a requirement for adapting anothers code to your purpose is that your starting data be sufficiently similar.

yes, apologies accepted. please educate me on what i m suppose to do

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.