Related
R has aov-function for anova. So if I want to perform it, i can write simple code:
df2=read.csv("C:/Users/Admin/Desktop/222.csv",sep=";", dec=",")
fit=aov(x1 ~ Q7,data=df2)
summary(fit)
I can perform 3 anova for each variables, if i three time write this code.
But how perform anova for all three variables at once? I.E. not writing this for each X.
df2=
structure(list(Q7 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"),
x1 = c(13L, 31L, 17L, 27L, 24L, 21L, 34L, 28L, 13L, 25L,
12L, 16L, 26L, 27L, 20L), x2 = c(32L, 29L, 16L, 27L, 27L,
22L, 36L, 35L, 22L, 27L, 13L, 30L, 17L, 27L, 9L), x3 = c(29L,
13L, 18L, 25L, 19L, 21L, 24L, 22L, 30L, 30L, 14L, 28L, 19L,
11L, 13L)), .Names = c("Q7", "x1", "x2", "x3"), class = "data.frame", row.names = c(NA,
-15L))
You can use reformulate() to come up with the formulas to be used within aov` function.
lapply(names(df2[-1]),function(x)aov(reformulate("Q7",x),data=df2))
You can also add summary in the code above
This question already has an answer here:
month language in the as.date function
(1 answer)
Closed 5 years ago.
My data frame is:
x=structure(list(V1 = structure(c(33L, 35L, 36L, 37L, 39L, 4L,
6L, 7L, 8L, 10L, 14L, 16L, 18L, 19L, 21L, 25L, 27L, 28L, 29L,
30L, 1L, 17L, 31L, 32L, 34L, 38L, 40L, 2L, 3L, 5L, 9L, 11L, 12L,
13L, 15L, 20L, 22L, 23L, 24L, 26L), .Label = c("1-Feb-71", "10-Feb-71",
"11-Feb-71", "11-Jan-71", "12-Feb-71", "12-Jan-71", "13-Jan-71",
"14-Jan-71", "15-Feb-71", "15-Jan-71", "16-Feb-71", "17-Feb-71",
"18-Feb-71", "18-Jan-71", "19-Feb-71", "19-Jan-71", "2-Feb-71",
"20-Jan-71", "21-Jan-71", "22-Feb-71", "22-Jan-71", "23-Feb-71",
"24-Feb-71", "25-Feb-71", "25-Jan-71", "26-Feb-71", "26-Jan-71",
"27-Jan-71", "28-Jan-71", "29-Jan-71", "3-Feb-71", "4-Feb-71",
"4-Jan-71", "5-Feb-71", "5-Jan-71", "6-Jan-71", "7-Jan-71", "8-Feb-71",
"8-Jan-71", "9-Feb-71"), class = "factor"), V2 = structure(c(1L,
15L, 2L, 4L, 3L, 5L, 10L, 5L, 7L, 12L, 8L, 16L, 16L, 22L, 16L,
19L, 22L, 12L, 17L, 23L, 24L, 24L, 21L, 17L, 19L, 16L, 6L, 11L,
9L, 25L, 25L, 8L, 5L, 13L, 20L, 18L, 16L, 13L, 12L, 14L), .Label = c("7.1359",
"7.1367", "7.1382", "7.1386", "7.1390", "7.1397", "7.1403", "7.1406",
"7.1410", "7.1411", "7.1412", "7.1414", "7.1418", "7.1420", "7.1422",
"7.1429", "7.1431", "7.1434", "7.1435", "7.1437", "7.1439", "7.1443",
"7.1445", "7.1465", "ND"), class = "factor")), .Names = c("V1",
"V2"), class = "data.frame", row.names = c(NA, -40L))
I am trying to convert column V1 to Date, but it is not working. Ive been looking some topics but it just doesnt work.
This my code:
x$V1 <- as.Date(x$V1, format="%d-%b-%y")
It works for some rows of V1 column but not for others.
Any help?
In my version of R, the conversion in your example only works for January and not for February. I think it is related to the language.
For example, in French, February is coded as fév and so Feb is not recognized.
Once I did:
x$V1=gsub("Feb", "fév", x$V1)
it worked.
It probably depends on which language your version of R uses.
I have adjacency list in the form of:
1. 3,4
2. 4
3. 1,4
4. 1,2,3
and I want to transform into adjacency matrix using R.
I have tried various commands like transformation of adjacency list to igraph object and then retransformation of igraph to adjacency matrix, but the obtained adjacency matrix is S4 class. I want simple commands to transform adjacency list to adjacency matrix in R.
data
list(c(1L, 3L, 4L, 8L, 14L, 31L, 2L, 29L, 33L, 7L, 11L, 17L,
5L, 6L, 34L), c(2L, 3L, 4L, 8L, 9L, 12L, 13L, 14L, 18L, 22L,
1L, 10L, 33L, 34L), c(2L, 3L, 4L, 8L, 9L, 12L, 13L, 14L, 18L,
20L, 22L, 32L, 1L, 31L, 34L, 24L), c(2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 1L, 31L, 10L, 28L,
29L), c(4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L,
22L, 32L, 1L, 17L), c(4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L,
14L, 18L, 20L, 22L, 32L, 1L, 17L), c(4L, 5L, 6L, 7L, 8L, 9L,
11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 1L, 17L), c(2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 1L,
31L, 10L, 28L, 29L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L,
13L, 14L, 18L, 20L, 22L, 32L, 10L, 28L, 29L, 33L, 34L, 15L, 16L,
19L, 21L, 23L, 24L, 30L, 31L, 27L), c(2L, 4L, 8L, 9L, 10L, 14L,
28L, 29L, 33L, 15L, 16L, 19L, 20L, 21L, 23L, 24L, 27L, 30L, 31L,
32L), c(4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L,
22L, 32L, 1L, 17L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L,
13L, 14L, 18L, 20L, 22L, 32L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L), c(2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 1L, 31L,
10L, 28L, 29L, 33L, 15L, 16L, 19L, 21L, 23L, 24L, 27L, 30L),
c(9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L, 31L, 32L, 10L, 14L,
20L, 27L, 28L, 29L), c(9L, 15L, 16L, 19L, 21L, 23L, 24L,
30L, 31L, 32L, 10L, 14L, 20L, 27L, 28L, 29L), c(1L, 7L, 11L,
17L, 5L, 6L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L,
13L, 14L, 18L, 20L, 22L, 32L, 31L), c(9L, 15L, 16L, 19L,
21L, 23L, 24L, 30L, 31L, 32L, 10L, 14L, 20L, 27L, 28L, 29L
), c(3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L,
20L, 22L, 32L, 31L, 10L, 15L, 16L, 19L, 21L, 23L, 24L, 27L,
28L, 29L, 30L), c(9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L,
31L, 32L, 10L, 14L, 20L, 27L, 28L, 29L), c(2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 31L
), c(9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L, 31L, 32L, 10L,
14L, 20L, 27L, 28L, 29L), c(24L, 25L, 32L, 3L, 34L, 27L,
33L, 9L, 15L, 16L, 19L, 21L, 23L, 30L, 31L, 10L, 14L, 20L,
28L, 29L), c(24L, 25L, 32L, 34L, 26L, 29L), c(26L, 28L, 30L,
33L, 34L, 32L, 25L, 29L), c(24L, 27L, 33L, 9L, 10L, 14L,
15L, 16L, 19L, 20L, 21L, 23L, 28L, 29L, 30L, 31L, 32L), c(4L,
8L, 9L, 10L, 14L, 28L, 29L, 33L, 26L, 30L, 32L, 15L, 16L,
19L, 20L, 21L, 23L, 24L, 27L, 31L), c(1L, 4L, 8L, 9L, 10L,
14L, 28L, 29L, 33L, 25L, 26L, 15L, 16L, 19L, 20L, 21L, 23L,
24L, 27L, 30L, 31L, 32L), c(26L, 28L, 30L, 33L, 34L, 9L,
15L, 16L, 19L, 21L, 23L, 24L, 31L, 32L, 10L, 14L, 20L, 27L,
29L), c(1L, 3L, 4L, 8L, 14L, 18L, 20L, 22L, 31L, 33L, 34L,
9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L, 32L, 10L, 27L, 28L,
29L), c(3L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L,
20L, 22L, 32L, 26L, 28L, 24L, 25L, 15L, 16L, 19L, 21L, 23L,
30L, 31L, 10L, 27L, 29L), c(1L, 2L, 9L, 10L, 14L, 28L, 29L,
33L, 31L, 34L, 26L, 30L, 24L, 27L), c(1L, 3L, 31L, 33L, 34L,
2L, 26L, 30L, 24L, 25L, 9L))
Suppose el is a list of edge list:
el = list(c(3,4),
c(2,4),
c(1,4),
c(1,2,3))
#Get the matrix dimension
dim <- length(el)
m <- sapply(el, function(x) { r<-rep(0,dim); r[unlist(x)]<-1;r})
[,1] [,2] [,3] [,4]
[1,] 0 0 1 1
[2,] 0 1 0 1
[3,] 1 0 0 1
[4,] 1 1 1 0
I am attempting to create and output as pdfs a list of 64 items. My data takes the form:
QQJAN List of 64
file1: List of 2
..$x: num [1:161] 96.7 96.8 97.5 ...
..$y: num [1:161] 9.3 10.3 17.3 ...
..................................................................
file64: List of 2
..$x: num [1:161] 42.6 59.9 70.4 ...
..$y: num [1:161] 9.3 10.3 17.3 ...
I can do this for any single item in the list using:
plot(QQJAN$file1)
and can then output these files to my working directory as pdfs, but how can all 64 files in the list be plotted and outputted with their names, i.e. file1.pdf, file 2.pdf etc.
Can the lapply function be used here?
A reproducible example:
QQJAN$file1$x=c(1,2,3,4)
QQJAN$file1$y=c(2,4,5,6)
QQJAN$file2$x=c(2,2,3,5)
QQJAN$file2$y=c(2,4,5,6)
Not tested due to lack of a reproducible example:
for (i in seq_along(QQJAN)) {
pdf(sprintf("plot%i.pdf", i)) #or pdf(paste0(names(QQJAN)[i], ".pdf"))
plot(QQJAN[[i]])
dev.off()
}
If you are only interested in side effects, such as plotting, a for loop is usually appropriate. You should use lapply if you need a return value.
We can use lapply to loop over the names of the list elements, create the pdf file by pasteing the individual names with .pdf, subset the list (QQJAN[[x]]), plot.
invisible(lapply(names(QQJAN), function(x) {
pdf(paste0(x, '.pdf'))
plot(QQJAN[[x]])
dev.off()}))
data
QQJAN <- structure(list(file1 = structure(list(x = c(6L, 5L, 15L, 11L,
14L, 19L, 6L, 16L, 17L, 6L, 13L, 8L, 14L, 14L, 7L, 19L, 4L, 1L,
11L, 3L, 2L, 12L, 15L, 3L, 5L, 14L, 2L, 12L, 13L, 1L, 7L, 5L,
8L, 3L, 19L, 5L, 15L, 13L, 14L, 20L), y = c(29L, 23L, 17L, 14L,
3L, 5L, 24L, 22L, 16L, 21L, 28L, 52L, 28L, 43L, 33L, 60L, 28L,
18L, 11L, 9L, 30L, 15L, 17L, 8L, 44L, 19L, 57L, 59L, 45L, 30L,
9L, 13L, 1L, 60L, 39L, 21L, 35L, 50L, 3L, 44L)), .Names = c("x",
"y")), file2 = structure(list(x = c(11L, 3L, 11L, 5L, 8L, 7L,
6L, 18L, 8L, 17L, 7L, 15L, 19L, 3L, 10L, 12L, 13L, 2L, 9L, 10L,
15L, 13L, 3L, 6L, 16L, 1L, 20L, 5L, 9L, 4L, 12L, 1L, 6L, 13L,
18L, 7L, 18L, 19L, 15L, 13L), y = c(56L, 31L, 40L, 43L, 20L,
45L, 55L, 8L, 43L, 26L, 7L, 52L, 7L, 31L, 11L, 14L, 55L, 26L,
4L, 42L, 34L, 44L, 12L, 4L, 30L, 60L, 23L, 44L, 29L, 55L, 6L,
37L, 11L, 14L, 36L, 52L, 28L, 22L, 31L, 33L)), .Names = c("x",
"y"))), .Names = c("file1", "file2"))
############ uncoded data
x10<- structure(c(0L, 0L, 0L, 0L, 1L, 1L, 1L, 5L, 8L, 9L, 31L, 1L,
0L, 0L, 0L, 1L, 0L, 1L, 2L, 7L, 2L, 10L, 0L, 2L, 0L, 2L, 2L,
5L, 2L, 4L, 6L, 8L, 4L, 1L, 1L, 3L, 2L, 2L, 6L, 1L, 12L, 18L,
7L, 29L, 8L, 4L, 6L, 8L, 6L, 19L, 3L, 9L, 12L, 3L, 12L, 14L,
1L, 2L, 1L, 3L, 1L, 0L, 4L, 6L, 3L, 11L, 0L, 0L, 0L, 1L, 3L,
7L, 5L, 8L, 21L, 26L, 51L, 0L, 1L, 0L, 3L, 5L, 10L, 9L, 29L,
55L, 60L, 125L, 3L, 0L, 1L, 1L, 3L, 10L, 1L, 6L, 18L, 17L, 13L,
6L, 3L, 4L, 13L, 6L, 33L, 17L, 48L, 84L, 54L, 103L, 34L, 11L,
20L, 27L, 26L, 50L, 29L, 30L, 54L, 28L, 34L, 31L, 5L, 7L, 3L,
4L, 20L, 8L, 16L, 16L, 8L, 41L, 1L, 0L, 0L, 3L, 1L, 3L, 3L, 11L,
19L, 16L, 56L, 0L, 0L, 0L, 0L, 3L, 11L, 3L, 18L, 25L, 21L, 62L,
3L, 0L, 1L, 4L, 2L, 7L, 8L, 15L, 22L, 12L, 19L, 5L, 2L, 8L, 9L,
9L, 42L, 18L, 51L, 70L, 45L, 103L, 29L, 15L, 23L, 34L, 25L, 57L,
23L, 38L, 55L, 30L, 33L, 36L, 5L, 5L, 6L, 6L, 16L, 6L, 10L, 17L,
9L, 35L, 2L, 0L, 1L, 1L, 2L, 4L, 6L, 8L, 22L, 33L, 73L, 0L, 0L,
0L, 1L, 2L, 7L, 7L, 15L, 27L, 21L, 56L, 1L, 2L, 2L, 0L, 2L, 9L,
4L, 8L, 24L, 13L, 17L, 14L, 2L, 8L, 10L, 16L, 51L, 16L, 51L,
69L, 29L, 99L, 44L, 18L, 25L, 34L, 19L, 49L, 26L, 43L, 63L, 15L,
30L, 42L, 9L, 17L, 7L, 3L, 16L, 8L, 13L, 22L, 18L, 45L, 0L, 0L,
1L, 3L, 0L, 7L, 4L, 14L, 15L, 20L, 47L, 0L, 1L, 0L, 1L, 1L, 3L,
3L, 5L, 6L, 11L, 21L, 1L, 0L, 0L, 4L, 2L, 3L, 8L, 7L, 17L, 3L,
13L, 5L, 2L, 6L, 13L, 15L, 34L, 19L, 42L, 62L, 37L, 83L, 52L,
16L, 26L, 26L, 29L, 53L, 28L, 45L, 45L, 15L, 22L, 26L, 8L, 12L,
11L, 5L, 12L, 5L, 7L, 17L, 10L, 28L), .Dim = c(11L, 6L, 5L), .Dimnames = structure(list(
c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"),
c("I've changed for work/ a new job/ gone on a work plan",
"I want a phone that doesn't offer", "I want Best Mates/ Favourites",
"I was offered or saw a better offer on another network",
"Issues with the network (poor coverage)", "Other"
), YearQuarter = c("2011-09-01", "2011-12-01", "2012-03-01",
"2012-06-01", "2012-09-01")), .Names = c("", "", "YearQuarter"
)), class = "table")
############ recoded data
x10 <- structure(c(40L, 3L, 13L, 12L, 3L, 9L, 12L, 13L, 10L, 36L, 16L,
30L, 15L, 54L, 21L, 14L, 22L, 10L, 77L, 16L, 29L, 185L, 28L,
84L, 30L, 19L, 24L, 157L, 82L, 132L, 62L, 197L, 84L, 49L, 78L,
32L, 72L, 11L, 30L, 83L, 17L, 43L, 31L, 25L, 37L, 148L, 93L,
121L, 63L, 206L, 93L, 44L, 80L, 27L, 106L, 16L, 30L, 77L, 17L,
42L, 30L, 20L, 32L, 128L, 117L, 120L, 45L, 215L, 106L, 63L, 102L,
35L, 67L, 15L, 29L, 32L, 9L, 11L, 16L, 18L, 24L, 120L, 94L, 104L,
37L, 230L, 90L, 38L, 79L, 24L), .Dim = c(3L, 6L, 5L), .Dimnames = structure(list(
c("Promoters", "Detractors", "Passive"), c("I've changed for work/ a new job/ gone on a work plan",
"I want a phone that doesn't offer", "I want Best Mates/ Favourites",
"I was offered or saw a better offer on another network",
"Issues with the network (poor coverage)", "Other"
), YearQuarter = c("2011-09-01", "2011-12-01", "2012-03-01",
"2012-06-01", "2012-09-01")), .Names = c("", "", "YearQuarter"
)), class = "table")
x10.p <- round(prop.table(x10,c(3,2)),2)*100
Hi there
The Net Promotion Score is a question which asks the consumers to rate the 'the likelihood to recommend the product or the service' on a zero to ten scale. People reported with 10 and 9 are called 'promoters', people rated 8 and 7 are seen as 'Passive', and people reported less than 6 are considered as detractors. The Net Promotion score is the difference between the percentage of 'Promoters' minus the the percentage of 'Detractors'.
I summerised and recoded the answers from the question into a table x10 from Sep 2011 to Sep 2012. The numbers are actual people counts for each group (Promoter,Detractor and Passive). Apologies for the three dimensioanl table, I am interested in the Net Promoter Score for each reason( i.e what's the percentage difference among the promoters and detractors for "I've changed for work/ a new job/ gone on a work plan" in Sep 2012.
The Net Promotion Score before I can plot it which requires a bit manipulation. I wonder if anyone knows to how do it?
Cheers
First, don't round until you've done all your calculations (otherwise you will have percentages not adding to 1)
x10.p <- prop.table(x10,c(3,2))*100
# get the total promoters
promoters <- apply(x10.p, 2:3, function(x) sum(tail(x,2)))
# and detractors
detractors <- apply(x10.p, 2:3, function(x) sum(head(x,7)))
# passive is everything else
passive <- passive <- 100 - (detractors +promoters)
# the net score
net <- promoters - detractors
net
YearQuarter
2011-09-01 2011-12-01 2012-03-01 2012-06-01 2012-09-01
I've changed for work/ a new job/ gone on a work plan 66.071429 50.00000 53.982301 59.210526 46.846847
I want a phone that doesn't offer 37.500000 52.86195 46.153846 44.117647 44.230769
I want Best Mates/ Favourites -2.857143 15.06849 6.451613 12.195122 -3.448276
I was offered or saw a better offer on another network 24.390244 20.21563 15.193370 3.013699 8.176101
Issues with the network (poor coverage) -43.333333 -39.35860 -39.502762 -46.448087 -54.061625
Other -17.391304 -18.23899 -23.841060 -19.500000 -29.078014
You want september 2012, select just that column, with drop = FALSE to ensure it is still a matrix with 1 column.
net[,'2012-09-01', drop = FALSE]
YearQuarter
2012-09-01
I've changed for work/ a new job/ gone on a work plan 46.846847
I want a phone that doesn't offer 44.230769
I want Best Mates/ Favourites -3.448276
I was offered or saw a better offer on another network 8.176101
Issues with the network (poor coverage) -54.061625
Other -29.078014