Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

r - Row-wise sum of values grouped by columns with same name

I have a data frame where several columns may have the same name. In this small example, both column "A" and "G" occur twice:

    A  C  G  A  G  T
1   1 NA NA NA  1 NA
2   1 NA  5  3  1 NA
3  NA  1 NA NA NA  1
4  NA NA  1  2 NA NA
5  NA NA  1  1 NA NA
6  NA  1 NA NA NA  1
7  NA  1 NA NA NA  1

I wish to create a data set with one column per column name. For each row, the individual column values should be replaced with the sum (sum(..., na.rm = TRUE)) of the values within each column name. For example, in row two, the two individual "A" values (1 and 3) should be replaced with 4. I don't know in advance which column names that occur several times.

The expected output would then be:

#     A  C  G  T
# 1   1  0  1  0
# 2   4  0  6  0
# 3   0  1  0  1
# 4   2  0  1  0
# 5   1  0  1  0
# 6   0  1  0  1
# 7   0  1  0  1

So I guess I could do something like:

noms = colnames(dat)
for(x in noms[duplicated(noms)]) {
  dat[ , x] = rowSums(dat[ , x == noms], na.rm = TRUE)
}
dat = dat[,!duplicated(noms)]

But this is a bit clunky and for loops are meant to be evil. Is there any way to do this more simply?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We can transpose dat , calculate rowsum per group (colnames of the original dat), then transpose the result back to original structure.

t(rowsum(t(dat), group = colnames(dat), na.rm = T))
#  A C G T
#1 1 0 1 0
#2 4 0 6 0
#3 0 1 0 1
#4 2 0 1 0
#5 1 0 1 0
#6 0 1 0 1
#7 0 1 0 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...