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)
Related
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 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)
This question already has answers here:
Filtering a data frame by values in a column [duplicate]
(3 answers)
Closed 3 years ago.
I have the following data with the ID of subjects.
V1
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2
11 2
12 2
13 2
14 2
15 2
16 4
17 4
18 4
19 4
20 4
21 4
22 4
23 4
24 4
I want to subset all the rows of the data where V1 == 4. This way I can see which observations relate to subject 4.
For example, the correct output would be
16 4
17 4
18 4
19 4
20 4
21 4
22 4
23 4
24 4
However, the output I'm given after subsetting does not give me the correct rows . It simply gives me.
V1
1 4
2 4
3 4
4 4
5 4
6 4
7 4
8 4
I'm unable to tell which observations relate to subject 4, as observations 1:8 are for subject 2.
I've tried the usual methods, such as
condition<- df == 4
df[condition]
How can I subset the data so I'm given back a dataset that shows the correct row numbers for subject 4.
You can also use the subset function:
subset(df,df$V1==4)
I've managed to find a solution since posting.
newdf <- subset(df, V1 == 4).
However i'm still very interested in other solutions to this problems, so please post if you're aware of another method.
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
I would like to import the data into R as intervals, then I would like to count all the numbers falling within these intervals and draw a histogram from this counts.
Example:
start end freq
1 8 3
5 10 2
7 11 5
.
.
.
Result:
number freq
1 3
2 3
3 3
4 3
5 5
6 5
7 10
8 10
9 7
10 7
11 5
Some suggestions?
Thank you very much!
Assuming your data is in df, you can create a data set that has each number in the range repeated by freq. Once you have that it's trivial to use the summarizing functions in R. This is a little roundabout, but a lot easier than explicitly computing the sum of the overlaps (though that isn't that hard either).
dat <- unlist(apply(df, 1, function(x) rep(x[[1]]:x[[2]], x[[3]])))
hist(dat, breaks=0:max(df$end))
You can also do table(dat)
dat
1 2 3 4 5 6 7 8 9 10 11
3 3 3 3 5 5 10 10 7 7 5