如何将R中的第一行更改为标题?
我有下表:如何将R中的第一行更改为标题?,r,columnheader,R,Columnheader,我有下表: X.5 X.6 X.7 X.8 X.9 X.10 X.11 X.12 X.13 17 Zip CuCurrent PaCurrent PoCurrent Contact Ext Fax email Status 18 74136 0 1 0 918-491-6998 0 918-491-6659
X.5 X.6 X.7 X.8 X.9 X.10 X.11 X.12 X.13
17 Zip CuCurrent PaCurrent PoCurrent Contact Ext Fax email Status
18 74136 0 1 0 918-491-6998 0 918-491-6659 1
19 30329 1 0 0 404-321-5711 1
20 74136 1 0 0 918-523-2516 0 918-523-2522 1
21 80203 0 1 0 303-864-1919 0 1
22 80120 1 0 0 345-098-8890 456 1
如何将第一行“zip、cucurrent、paccurrent…”作为列标题
谢谢
下面是dput(dat)
dput(dat)
结构(列表X.5=结构(c(26L,14L,6L,14L,17L,16L),.Label=c(“,
“1104”、“1234我不知道大街”、“139.98”、“摩根街300号”,
“30329”、“312.95”、“特拉夫南四街4101号”、“北89号公路500号”,
"644.04", "656.73", "72160", "72336-7000", "74136", "75501",
“80120”、“80203”、“877.87”、“地址1”、“BZip”、“Svcs总管理员(WPY)”,
“InvFileName2”、“LDC组织成本”、“不适用”、“空”、“Zip”)、class=“factor”),
X.6=结构(c(7L,2L,3L,3L,2L,3L),.Label=c(“,
“0”、“1”、“301西南第七街”、“800-688-6160”、“地址2”、“电流”,
“紧急情况”、“LDC成本调整”、“Mtelemetry”、“不适用”、“空值”,
“套件1402”,class=“factor”),X.7=结构(c(8L,3L,
标签=c(“,”0“,”1“,”地址3“,”客户“,
“土地发展公司杂项费用”、“空”、“当前”、“7512室”)、class=“factor”),
X.8=结构(c(14L,2L,2L,2L,2L,2L),.Label=c(“,
“0”、“100.98”、“237.02”、“242.33”、“335.04”、“50.6”、“城市”,
“达勒姆”、“LDC_FinalVolume”、“莱文沃斯”、“大客户”,
“Petersburg”、“PoCurrent”、“Prescott”、“Washington”),class=“factor”),
X.9=结构(c(18L,16L,10L,17L,7L,9L),.Label=c(“,
"0", "1", "139.98", "20024", "27701", "303-864-1919", "312.95",
"345-098-8890", "404-321-5711", "644.04", "656.73", "66048",
“86313”、“877.87”、“918-491-6998”、“918-523-2516”、“联系人”,
“LDC_FinalCost”、“PoCustomer”、“Zip”)、class=“factor”),
X.10=结构(c(14L,2L,1L,2L,2L,9L),标签=c(“,
"0", "2.620194604", "2.710064788", "2.717239052", "2.766403162",
"202-708-4995", "3.09912854", "456", "804-504-7200", "913-682-2000",
“919-956-5541”、“928-717-7472”、“分机”、“发票所需”,
“LDC_单价”、“空”、“电话”),class=“factor”),X.11=结构(c(7L,
标签=c(“,”,“1067”,“918-491-6659”,
“918-523-2522”、“分机”、“传真”、“发票月份”、“最不发达国家单价原件”,
“NULL”,“x2951”,class=“factor”),X.12=结构(c(13L,
标签=c(“,”0“,”100.98“,”202-401-3722“,
"237.02", "242.33", "335.04", "50.6", "716- 344-3303", "804-504-7227",
“913-758-4230”、“919-956-7152”、“电子邮件”、“传真”、“GSA”,
“辅助体积”,class=“factor”),X.13=结构(c(10L,2L,
标签=c(“,”1“,”15“,”202-497-6164“,
“3”、“804-504-7200”、“紧急”、“主要类型ID”、“空”,
“状态”,“支持量”,class=“factor”),.Names=c(“X.5”,
“X.6”、“X.7”、“X.8”、“X.9”、“X.10”、“X.11”、“X.12”、“X.13”),row.names=17:22,class=“data.frame”)
如果从csv文件中获取,请使用read.csv中的参数“header”
dat=read.csv("gas.csv", header=TRUE)
如果您已经拥有数据,并且不想/或者无法以干净的方式获取数据,那么您可以这样做
dat=structure(list(X.5 = structure(c(26L, 14L, 6L, 14L, 17L, 16L), .Label = c("", "1104", "1234 I don't know Ave.", "139.98", "300 Morgan St.", "30329", "312.95", "4101 S. 4th Street, Traff", "500 Highway 89 North", "644.04", "656.73", "72160", "72336-7000", "74136", "75501", "80120", "80203", "877.87", "Address1", "BZip", "General Svcs Admin (WPY)", "InvFileName2", "LDC_Org_Cost", "N/A", "NULL", "Zip"), class = "factor"), X.6 = structure(c(7L, 2L, 3L, 3L, 2L, 3L), .Label = c("", "0", "1", "301 7th St. SW", "800-688-6160", "Address2", "CuCurrent", "Emergency", "LDC_Cost_Adj", "Mtelemetry", "N/A", "NULL", "Suite 1402"), class = "factor"), X.7 = structure(c(8L, 3L, 2L, 2L, 3L, 2L), .Label = c("", "0", "1", "Address3", "Cucustomer", "LDC_Misc_Fee", "NULL", "PaCurrent", "Room 7512"), class = "factor"), X.8 = structure(c(14L, 2L, 2L, 2L, 2L, 2L), .Label = c("", "0", "100.98", "237.02", "242.33", "335.04", "50.6", "City", "Durham", "LDC_FinalVolume", "Leavenwoth", "Pacustomer", "Petersburg", "PoCurrent", "Prescott", "Washington"), class = "factor"), X.9 = structure(c(18L, 16L, 10L, 17L, 7L, 9L), .Label = c("", "0", "1", "139.98", "20024", "27701", "303-864-1919", "312.95", "345-098-8890", "404-321-5711", "644.04", "656.73", "66048", "86313", "877.87", "918-491-6998", "918-523-2516", "Contact", "LDC_FinalCost", "PoCustomer", "Zip"), class = "factor"), X.10 = structure(c(14L, 2L, 1L, 2L, 2L, 9L), .Label = c("", "0", "2.620194604", "2.710064788", "2.717239052", "2.766403162", "202-708-4995", "3.09912854", "456", "804-504-7200", "913-682-2000", "919-956-5541", "928-717-7472", "Ext", "InvoicesNeeded", "LDC_UnitPrice", "NULL", "Phone"), class = "factor"), X.11 = structure(c(7L, 4L, 1L, 5L, 1L, 1L), .Label = c("", " ", "1067", "918-491-6659", "918-523-2522", "Ext", "Fax", "InvoiceMonths", "LDC_UnitPrice_Original", "NULL", "x2951"), class = "factor"), X.12 = structure(c(13L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "0", "100.98", "202-401-3722", "237.02", "242.33", "335.04", "50.6", "716- 344-3303", "804-504-7227", "913- 758-4230", "919- 956-7152", "email", "Fax", "GSA", "Supp_Vol"), class = "factor"), X.13 = structure(c(10L, 2L, 2L, 2L, 2L, 2L), .Label = c("", "1", "15", "202-497-6164", "3", "804-504-7200", "Emergency", "MajorTypeId", "NULL", "Status", "Supp_Vol_Adj"), class = "factor")), .Names = c("X.5", "X.6", "X.7", "X.8", "X.9", "X.10", "X.11", "X.12", "X.13"), row.names = 17:22, class = "data.frame")
dat2 = dat[2:6,]
colnames(dat2) = dat[1,]
dat2
如果您不想将数据重新读取到R中(从注释中似乎没有),可以执行以下操作。我必须加上一些零才能让你的数据完全读取,所以忽略这些
dat
## V2 V3 V4 V5 V6 V7 V8 V9 V10
## 17 Zip CuCurrent PaCurrent PoCurrent Contact Ext Fax email Status
## 18 74136 0 1 0 918-491-6998 0 918-491-6659 0 1
## 19 30329 1 0 0 404-321-5711 0 0 0 1
## 20 74136 1 0 0 918-523-2516 0 918-523-2522 0 1
## 21 80203 0 1 0 303-864-1919 0 0 0 1
## 22 80120 1 0 0 345-098-8890 456 0 0 1
首先,将第一行作为列名。接下来删除第一行。通过将列转换为相应的类型来完成它
names(dat) <- as.matrix(dat[1, ])
dat <- dat[-1, ]
dat[] <- lapply(dat, function(x) type.convert(as.character(x)))
dat
## Zip CuCurrent PaCurrent PoCurrent Contact Ext Fax email Status
## 1 74136 0 1 0 918-491-6998 0 918-491-6659 0 1
## 2 30329 1 0 0 404-321-5711 0 0 0 1
## 3 74136 1 0 0 918-523-2516 0 918-523-2522 0 1
## 4 80203 0 1 0 303-864-1919 0 0 0 1
## 5 80120 1 0 0 345-098-8890 456 0 0 1
names(dat)将数据导入R时,请使用header=TRUE
如果您能够从文件中将数据重新读取到R中,您也可以将“skip”参数添加到read.csv中,跳过前16行,并使用第17行作为标题:
dat=read.csv("contacts.csv", skip=16, nrows=5, header=TRUE)
这可以通过一种简单的方式实现:
步骤1:将第一行复制到标题:
names(dat) <- dat[1,]
names(dat)实现这一点最干净的方法是一个已经为此目的设计的简单函数。
你需要一个清洁包
janitor::row_to_names(dat)
如果希望第n行用于列名,则函数的第二个参数是要使用的行号。默认值为1。Adput()
。但是你可以做一个colnames(dat)@RichardScriven很好的论点。回想起来,在read..
函数中,这看起来确实像是缺少header=TRUE
,但是为什么行名从17开始呢?@hrbrmstras.character(dat[1,])
返回第一行中因子级别的数字索引(作为字符)。我不知道为什么,奇怪。我确保它在一个巨大的netflow记录表上做了我认为应该做的事情。啊。但这些都不是(刚刚检查过的)因素。希望我们有一个dput()
从:-)@RichardScriven开始工作,dat
是csv文件gas
的子集。我用这个代码来子集dat=gas[17:22,7:15]
。我的原始帖子中的第17行应该是dat
的标题,但我不确定如何更改。我在导入csv
时使用了header=T
,您提供的数据框没有22行。。。你的问题提得不恰当。现在我已经更改了答案,因为您提供了一个有用的dputOne,即图书馆看门人的clean_name
,以避免lappy
语句。所以dat默认值对我来说似乎不是1。可能需要行数=1
这显然是最简单、最好的答案。谢谢
janitor::row_to_names(dat)