R:根据特定规则重塑数据帧

时间:2019-06-14 16:15:52

标签: r tidyr

以下是带有这些列的数据框

名称(本地数据)

  [1] "Frame"          "Time..Seconds." "X"              "Y"              "Z"              "W"              "X.1"           
  [8] "Y.1"            "Z.1"            "X.33"           "X.2"            "Y.2"            "Z.2"            "X.34"          
 [15] "X.3"            "Y.3"            "Z.3"            "X.35"           "X.4"            "Y.4"            "Z.4"           
 [22] "X.36"           "X.5"            "Y.5"            "Z.5"            "X.6"            "Y.6"            "Z.6"           
 [29] "X.7"            "Y.7"            "Z.7"            "X.8"            "Y.8"            "Z.8"            "X.9"           
 [36] "Y.9"            "Z.9"            "X.10"           "Y.10"           "Z.10"           "X.11"           "Y.11"          
 [43] "Z.11"           "X.12"           "Y.12"           "Z.12"           "X.13"           "Y.13"           "Z.13"          
 [50] "X.14"           "Y.14"           "Z.14"           "X.15"           "Y.15"           "Z.15"           "X.16"          
 [57] "Y.16"           "Z.16"           "X.17"           "Y.17"           "Z.17"           "X.18"           "Y.18"          
 [64] "Z.18"           "X.19"           "Y.19"           "Z.19"           "X.20"           "Y.20"           "Z.20"          
 [71] "X.21"           "Y.21"           "Z.21"           "X.22"           "Y.22"           "Z.22"           "X.23"          
 [78] "Y.23"           "Z.23"           "X.24"           "Y.24"           "Z.24"           "X.25"           "Y.25"          
 [85] "Z.25"           "X.26"           "Y.26"           "Z.26"           "X.27"           "Y.27"           "Z.27"          
 [92] "X.28"           "Y.28"           "Z.28"           "X.29"           "Y.29"           "Z.29"           "X.30"          
 [99] "Y.30"           "Z.30"           "X.31"           "Y.31"           "Z.31"           "X.32"           "Y.32"          
[106] "Z.32"     

我想将所有X,Y,Z列合并在一起。

我可以像这样选择所有X,Y和Z列名称:

Xpos <- c(2, grep(glob2rx("X*"), names(local_data)))

我可以像这样合并这些选定的列:

df.x <- local_data[,Xpos] %>% gather(key, xpox, 2:length(Xpos))

问题是有一些松散的X列没有对应的Y和Z列。例如X.33

我正在寻找一种基于矢量的优雅代码,以标识具有相应Y和X列的X列。从概念上讲,我认为最简单的方法是查找具有相同编号的列,例如X.10,Y.10,Z.10。有提示吗?

谢谢

1 个答案:

答案 0 :(得分:1)

这是您要搜索的内容吗? 我尽力整理一下。

  AbstractSpringSerialPortConnector ard;
    @RequestMapping("/hh")
public void hh() throws NoSuchPortException, TooManyListenersException, IOException {

            ard.connect();
            ard.sendMessage("try");

    }