简体   繁体   English

如何提取部分名称作为数据框中的新列名称

[英]How to extract out a partial name as new column name in a data frame

I have a data frame as below. 我有如下数据框。 I would like to take a part of column name as new name since it was too long and not necessary to me. 我想将列名的一部分作为新名称,因为它太长了,对我来说不是必需的。 Thanks for any helps. 感谢您的帮助。

df = read.table(text="PV_556933.C25E8ACXX.1.250197415   PD_556996.C25E8ACXX.1.250197421 PT_556997.MERGE PC_559379.D25PAACXX.2.250194617
G   G   G   G
A   A   A   A
A   A   A   A
G   G   G   T
C   T   C   T
A   A   A   A", header=T, stringsAsFactors=F)

I expect the data frame as:
PV_556933   PD_556996   PT_556997   PC_559379
G   G   G   G
A   A   A   A
A   A   A   A
G   G   G   T
C   T   C   T
A   A   A   A

How's this? 这个怎么样? I split the string ( strsplit ) at a dot (which you need to escape using \\\\ ) and take out only the first element using lapply . 我将字符串( strsplit )分割为一个点(您需要使用\\\\进行转义),并使用lapply仅取出第一个元素。 unlist is there to coerce the list into a vector. unlist可以将列表强制为向量。

> names(df) <- unlist(lapply(strsplit(names(df), "\\."), "[[", 1))
> df
  PV_556933 PD_556996 PT_556997 PC_559379
1         G         G         G         G
2         A         A         A         A
3         A         A         A         A
4         G         G         G         T
5         C         T         C         T
6         A         A         A         A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM