[英]R data.frame : headers based on existing row containing text and numbers
由於某些特定於我的R程序的原因,我想根據R中數據框中的現有列和行來分配列名和行名。也就是說,第一行必須成為列名,第一列有成為行名。
我首先想到的很簡單,使用:
colnames(myDataFrame) <- myDataFrame[1,]
rownames(MyDataFrame) <- myDataFrame[,1]
因為它也寫在這個主題中 。
但是我的數據框的第一行和第一列有很多情況要處理:只有文本,帶有數字的文本,文本或數字...這就是為什么這有時不起作用。 查看第一行中僅包含文本的示例:
我首先加載我的數據框,沒有任何標題:
> tab <- read.table(file, header = FALSE, sep = "\t")
> tab
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 TEST this is only text hoping it will work
2 I 4 0 0 0 0 0 0 1
3 really 7 6 6 3 10 6 10 10
4 hope 187 141 140 129 130 157 138 168
這是我的數據框,沒有行名和列名。 我希望“TEST這只是文本,希望它能夠工作”成為我的專欄名稱。 這個做法不起作用:
> colnames(tab) <- tab[1,]
> tab
2 10 9 9 10 8 9 8 9
1 TEST this is only text hoping it will work
2 I 4 0 0 0 0 0 0 1
3 really 7 6 6 3 10 6 10 10
4 hope 187 141 140 129 130 157 138 168
雖然這有效:
> colnames(tab) <- as.character(unlist(tab[1,]))
> tab
TEST this is only text hoping it will work
1 TEST this is only text hoping it will work
2 I 4 0 0 0 0 0 0 1
3 really 7 6 6 3 10 6 10 10
4 hope 187 141 140 129 130 157 138 168
我認為問題是因為R有時會將第一列或第一行視為因素。 但正如你所看到的:
> is.factor(tab[1,])
FALSE
即使它沒有被R轉換為因子,它也會失敗。
我試圖在我的程序中提示“as.character(unlist()))”,但在我可能遇到的其他一些情況下,它不再有效!...在第一行中查看帶有文本和數字的示例:
> otherTab <- read.table(otherFile, header = FALSE, sep = "\t")
> otherTab
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 TEST this45 is 486text 725 with ca257 some numbers
2 number45 4 0 0 0 0 0 0 1
3 254every 7 6 6 3 10 6 10 10
4 where 187 141 140 129 130 157 138 168
> colnames(otherTab) <- as.character(unlist(otherTab[1,]))
> otherTab
6 10 9 7 725 8 9 8 9
1 TEST this45 is 486text 725 with ca257 some numbers
2 number45 4 0 0 0 0 0 0 1
3 254every 7 6 6 3 10 6 10 10
4 where 187 141 140 129 130 157 138 168
那么如何以一種簡單的方式處理這些不同的情況(因為這似乎是一個如此簡單的問題)? 提前謝謝了。
發生這種情況是因為,在您的初始數據框中, V5
是“int”類型的列,而不是因素(因此您的第一行中有兩種不同的類型)
#> str(df)
#'data.frame': 4 obs. of 9 variables:
# $ V1: Factor w/ 4 levels "254every","TEST",..: 2 3 1 4
# $ V2: Factor w/ 4 levels "187","4","7",..: 4 2 3 1
# $ V3: Factor w/ 4 levels "0","141","6",..: 4 1 3 2
# $ V4: Factor w/ 4 levels "0","140","486text",..: 3 1 4 2
# $ V5: int 725 0 3 129
# $ V6: Factor w/ 4 levels "0","10","130",..: 4 1 2 3
# $ V7: Factor w/ 4 levels "0","157","6",..: 4 1 3 2
# $ V8: Factor w/ 4 levels "0","10","138",..: 4 1 2 3
# $ V9: Factor w/ 4 levels "1","10","168",..: 4 1 2 3
矢量的所有元素必須是相同的類型 。 當你嘗試unlist()
並將值存儲在向量中以傳遞給colnames()
,實際上傳遞了一個“int”向量(因為R將元素colnames()
轉換為公共類型):
#> str(unlist(df[1,]))
# Named int [1:9] 2 4 4 3 725 4 4 4 4
# - attr(*, "names")= chr [1:9] "V1" "V2" "V3" "V4" ...
如果修改數據框的結構以指定列V5
是一個因子,那么您的初始方法將起作用:
df[,5] <- as.factor(df[,5])
colnames(df) <- unlist(df[1,])
你會得到:
#> df
# TEST this45 is 486text 725 with ca257 some numbers
#1 TEST this45 is 486text 725 with ca257 some numbers
#2 number45 4 0 0 0 0 0 0 1
#3 254every 7 6 6 3 10 6 10 10
#4 where 187 141 140 129 130 157 138 168
如果您不想修改列類型,可以在強制轉換為向量並傳遞給colnames()
之前將as.character()
應用於第一行的每個元素:
colnames(df) <- lapply(df[1,], as.character)
結果如下:
#> df
# TEST this45 is 486text 725 with ca257 some numbers
#1 TEST this45 is 486text 725 with ca257 some numbers
#2 number45 4 0 0 0 0 0 0 1
#3 254every 7 6 6 3 10 6 10 10
#4 where 187 141 140 129 130 157 138 168
數據
structure(list(V1 = structure(c(2L, 3L, 1L, 4L), .Label = c("254every",
"TEST", "number45", "where"), class = "factor"), V2 = structure(c(4L,
2L, 3L, 1L), .Label = c("187", "4", "7", "this45"), class = "factor"),
V3 = structure(c(4L, 1L, 3L, 2L), .Label = c("0", "141",
"6", "is"), class = "factor"), V4 = structure(c(3L, 1L, 4L,
2L), .Label = c("0", "140", "486text", "6"), class = "factor"),
V5 = c(725L, 0L, 3L, 129L), V6 = structure(c(4L, 1L, 2L,
3L), .Label = c("0", "10", "130", "with"), class = "factor"),
V7 = structure(c(4L, 1L, 3L, 2L), .Label = c("0", "157",
"6", "ca257"), class = "factor"), V8 = structure(c(4L, 1L,
2L, 3L), .Label = c("0", "10", "138", "some"), class = "factor"),
V9 = structure(c(4L, 1L, 2L, 3L), .Label = c("1", "10", "168",
"numbers"), class = "factor")), .Names = c("V1", "V2", "V3",
"V4", "V5", "V6", "V7", "V8", "V9"), class = "data.frame", row.names = c("1",
"2", "3", "4"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.