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
178 views
in Technique[技术] by (71.8m points)

r - Find differences in new values

I have a data frame like this:

   id    colA    colB   colC    colD    colE colA_new colB_new colC_new colD_new colE_new NA NA
1  60 -0.6647 -1.6964 4.1104 -1.6663  0.9020   0.0610   0.6573  -1.6561   0.2499   1.1265  3  2
2  91 -0.0275 -1.3851 2.9980 -1.0789  0.5417  -0.1321  -1.8156   3.3495  -1.5437   0.2224  1  3
3 377 -0.6311 -1.1428 3.4623 -1.6608  0.2651  -0.6613  -1.1072   3.1694  -1.3843   0.5074  3  1
4 419  0.1328 -1.4191 1.9545 -1.0137 -0.4998  -0.9655  -1.6147   4.7327  -1.8279   0.9983  1  3
5 893 -0.4559 -1.2979 3.5166 -1.4022  0.7200  -0.3879  -1.7412   3.7275  -1.4870   0.4906  1  3
6 905 -1.0208 -1.4410 3.9912 -1.6189  1.0902  -0.3222  -0.8048   3.0135  -1.1330   0.3672  3  1

DATA

dframe <- structure(list(c(60, 91, 377, 419, 893, 905), c(-0.6647, -0.0275000000000001, 
-0.6311, 0.1328, -0.4559, -1.0208), c(-1.6964, -1.3851, -1.1428, 
-1.4191, -1.2979, -1.441), c(4.1104, 2.998, 3.4623, 1.9545, 3.5166, 
3.9912), c(-1.6663, -1.0789, -1.6608, -1.0137, -1.4022, -1.6189
), c(0.902, 0.5417, 0.2651, -0.4998, 0.72, 1.0902), c(0.061, 
-0.1321, -0.6613, -0.9655, -0.3879, -0.3222), c(0.6573, -1.8156, 
-1.1072, -1.6147, -1.7412, -0.8048), c(-1.6561, 3.3495, 3.1694, 
4.7327, 3.7275, 3.0135), c(0.2499, -1.5437, -1.3843, -1.8279, 
-1.487, -1.133), c(1.1265, 0.2224, 0.5074, 0.9983, 0.4906, 0.3672
), structure(c(3, 1, 3, 1, 1, 3), label = "TwoStep Cluster Number", labels = c(`Outlier Cluster` = -1), class = "haven_labelled"), 
    structure(c(2, 3, 1, 3, 3, 1), label = "TwoStep Cluster Number", labels = c(`Outlier Cluster` = -1), class = "haven_labelled")), .Names = c("id", 
"colA", "colB", "colC", "colD", "colE", "colA_new", "colB_new", 
"colC_new", "colD_new", "colE_new", NA, NA), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

id contains unique values. I want to find changes in scores for every pair of variables such as colA-colA_new, colB-colB_new. How can I have find the changes in scores for each pair and create new columns?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

One way using base R is to split the columns based on common part of the names and subtract the two columns.

cbind(dframe[1], sapply(split.default(dframe[-1], 
         sub("_new", "", names(dframe[-1]))), function(x) x[[2]] - x[[1]]))


#   id    colA    colB    colC    colD    colE
#1  60  0.7257  2.3537 -5.7665  1.9162  0.2245
#2  91 -0.1046 -0.4305  0.3515 -0.4648 -0.3193
#3 377 -0.0302  0.0356 -0.2929  0.2765  0.2423
#4 419 -1.0983 -0.1956  2.7782 -0.8142  1.4981
#5 893  0.0680 -0.4433  0.2109 -0.0848 -0.2294
#6 905  0.6986  0.6362 -0.9777  0.4859 -0.7230

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

Just Browsing Browsing

[2] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...