d*******1 发帖数: 854 | 1 R的书看了一些,但是具体到做事情还是没有头绪。 下面是问题:
有一个data:
subj gene value
1 xxx 10
1 yyy 11
1 zzz 12
2 xxx 34
2 yyy 45
2 zzz 67
在这里想要用gene xxx的value 对其他gene 的value 做regression (regression by
gene), 第一步先要merge 成下面那样的:
subj gene value xxx_value
1 xxx 10 10
1 yyy 11 10
1 zzz 12 10
2 xxx 34 34
2 yyy 45 34
2 zzz 67 34
谢谢 | a********s 发帖数: 188 | | D*********2 发帖数: 535 | 3 我也初学,给个 dirty way
m = number of types of gene, in your case, 3
n = number of subjects, in your case, 2
xxx_value <- rep(value[gene=="xxx"], rep(m, n)) | D******n 发帖数: 2836 | 4 dat$xvalue<-rep(subset(dat,gene=='xxx')$value,each=3)
【在 a********s 的大作中提到】 : 用rep() function生成新的列吧
| d*******1 发帖数: 854 | 5 这个data subj 和gene的次序可能都是乱的
【在 D*********2 的大作中提到】 : 我也初学,给个 dirty way : m = number of types of gene, in your case, 3 : n = number of subjects, in your case, 2 : xxx_value <- rep(value[gene=="xxx"], rep(m, n))
| d*******1 发帖数: 854 | 6 thanks. you use each=3, but i do not know the # of gene for each subj (they
may be different for each subj)
【在 D******n 的大作中提到】 : dat$xvalue<-rep(subset(dat,gene=='xxx')$value,each=3)
| m********n 发帖数: 11 | 7 d1 = read.table('tmp.txt', header = TRUE, sep = ' ')
d2 = d1[d1$gene == 'xxx', c('subj', 'value')]
names(d2)[2] = 'xxx_value'
merge(d1, d2, by = 'subj') |
|