I would like to generate a list of all combinations of different elements. I found the expand.grid function which seems to be doing a great job.
lCombi<-expand.grid(c(1,2),c(4,5,6),c(7,8))
Var1 Var2 Var3
1 1 4 7
2 2 4 7
3 1 5 7
4 2 5 7
5 1 6 7
6 2 6 7
7 1 4 8
8 2 4 8
9 1 5 8
10 2 5 8
11 1 6 8
12 2 6 8
Now, if I understand well, this is a list of 3 elements of 12 rows. But what I would like is a list of 12 elements of 3. I tried to transpose it, convert it into a matrix then back to a list but none worked. So I used my pre-R way of doing things and came up with :
iLength=nrow(lCombi)
lResult<-list()
for(i in 1:iLength){
lResult[[i]]<-lCombi[i,]
}
to get my lResult list, which contains what I want. Could you please tell me the more efficient way of doing this please ?
The split function is great at chopping up a data.frame in to a list of data.frames:
lResult <- split(lCombi, 1:nrow(lCombi))
If you'd like each list element to be a vector instead, use
lapply(lResult, unlist)
Related
guys:
I have two matrix as following:
d <- cbind(c(1,2,3,4),c(1,1,1,1),c(1,2,4,8))
v <- cbind(c(2,2,2,2),c(3,3,3,3))
But I want to get a matrix consisted of divj as following:
d1v1 d1v2 d2v1 d2v2 d3v1 d3v2
2 3 2 3 2 3
4 6 2 3 4 6
6 9 2 3 8 12
8 12 2 3 16 24
This is an example of my question,I wonder if you can tell me how to write codes to solve this question.Many thanks.
matrix(apply(v,2,function(x){x*d}),4,6)
I am trying to create an index for a data frame. Each team playing has its own row, but I would like to add a column to use as an index so that the first two teams have the index 'Game 0', the next two teams have the index 'Game 1' until the length of half the list. In python the code would look as follows:
for i in range(0,int(len(teams)/2)):
gamenumber.append('Game '+str(i))
gamenumber.append('Game '+str(i))
I am unfamiliar with R so any help would be appreciated!
This will give you a list of paired index numbers:
> teams=1:100
> data.frame("Games"=sort(c(1:(length(teams)/2), 1:(length(teams)/2))))
Games
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4
9 5
10 5
11 6
12 6
13 7
14 7
15 8
16 8
17 9
18 9
19 10
20 10 #etc.
Assuming teams is a data.frame with an even number of rows:
rep(1:(nrow(teams)/2), each=2)
I'm trying to merge 7 complete data frames into one great wide data frame. I figured I have to do this stepwise and merge 2 frames into 1 and then that frame into another so forth until all 7 original frames becomes one.
fil2005: "ID" "abr_2005" "lop_2005" "ins_2005"
fil2006: "ID" "abr_2006" "lop_2006" "ins_2006"
But the variables "abr_2006" "lop_2006" "ins_2006" and 2005 are all either 0,1.
Now the things is, I want to either merge or do a dcast of some sort (I think) to make these two long data frames into one wide data frame were both "abr_2005" "lop_2005" "ins_2005" and abr_2006" "lop_2006" "ins_2006" are in that final file.
When I try
$fil_2006.1 <- merge(x=fil_2005, y=fil_2006, by="ID__", all.y=T)
all the variables with _2005 at the end if it is saved to the fil_2006.1, but the variables ending in _2006 doesn't.
I'm apparently doing something wrong. Any idea?
Is there a reason you put those underscores after ID__? Otherwise, the code you provided will work
An example:
dat1 <- data.frame("ID"=seq(1,20,by=2),"varx2005"=1:10, "vary2005"=2:11)
dat2 <- data.frame("ID"=5:14,"varx2006"=1:20, "vary2006"=21:40)
# create data frames of differing lengths
head(dat1)
ID varx2005 vary2005
1 1 1 2
2 3 2 3
3 5 3 4
4 7 4 5
5 9 5 6
6 11 6 7
head(dat2)
ID varx2006 vary2006
1 5 1 21
2 6 2 22
3 7 3 23
4 8 4 24
5 9 5 25
6 10 6 26
merged <- merge(dat1,dat2,by="ID",all=T)
head(merged)
ID varx2006 vary2006 varx2005 vary2005
1 1 NA NA 1 2
2 3 NA NA 2 3
3 5 1 21 3 4
4 5 11 31 3 4
5 7 13 33 4 5
6 7 3 23 4 5
Consider some vector in R: x
x<-1:10
I'd like to create a repeating sequence of x, with the first element of each sequence truncated with each repetition, yielding the same output as would be given by issuing the following command in R:
c(1:10,2:10,3:10,4:10,5:10,6:10,7:10,8:10,9:10,10:10)
Can this be done? In reality, I'm working with a much larger vector for x. I'm playing with numerous combinations of the rep() function, to no avail.
Here's an alternative using mapply:
unlist(mapply(":", 1:10, 10))
# [1] 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 4 5 6 7
# [25] 8 9 10 4 5 6 7 8 9 10 5 6 7 8 9 10 6 7 8 9 10 7 8 9
# [49] 10 8 9 10 9 10 10
A bit of a hack, because you can decompose what you are trying to do into two sequences:
rep(0:9, 10:1) + sequence(10:1)
You can see what each part does. I don't know if there is a way to feed the parameters to rep() or seq() like you would do in a Python expansion.
unlist(sapply(1:10, function(x) { x:10 }))
I printed out the summary of a column variables as such:
Please see below the summary table printed out from R:
I would like to generate it into a data.frame. However, there are too many subject names that it's very difficult to list out all, also, the term "OTHER" with number 31 means that there are 319 subjects which appear only 1 time in the original data.frame.
So, the new data.frame I hope to produce would look like below:
Here is one possible solution.
Table<-table(rpois(100,5))
as.data.frame(Table)
Var1 Freq
1 1 2
2 2 11
3 3 9
4 4 18
5 5 13
6 6 20
7 7 14
8 8 8
9 9 3
10 10 1
11 11 1