使用 "LETTERS" 向数据集添加一行
Using "LETTERS" to add a row to dataset
我有一个包含 301 列的数据集。我想添加一行,其中包含一个以 a 开头的唯一字母并重复直到最后一列。我有的是:
DF:
Var1 Var2.....Var78 Var79...Var130V Var131
我想要的额外行是:
DF:
Var1 Var2.....Var78 Var79...Var130V Var131
NR A B CA CB EA EB
我试过了
DF[[nrow(DF)+1,]<- rep(letters)
DF[nrow(DF)+1,]<- paste(letters:ncol)
如有任何帮助,我们将不胜感激!
您可以使用此 one-liner:
在“Excel”列中获得相同的唯一字母 headers
c(LETTERS, c(sapply(LETTERS, paste0, LETTERS)))[1:301]
#> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
#> [14] "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
#> [27] "AA" "AB" "AC" "AD" "AE" "AF" "AG" "AH" "AI" "AJ" "AK" "AL" "AM"
#> [40] "AN" "AO" "AP" "AQ" "AR" "AS" "AT" "AU" "AV" "AW" "AX" "AY" "AZ"
#> [53] "BA" "BB" "BC" "BD" "BE" "BF" "BG" "BH" "BI" "BJ" "BK" "BL" "BM"
#> [66] "BN" "BO" "BP" "BQ" "BR" "BS" "BT" "BU" "BV" "BW" "BX" "BY" "BZ"
#> [79] "CA" "CB" "CC" "CD" "CE" "CF" "CG" "CH" "CI" "CJ" "CK" "CL" "CM"
#> [92] "CN" "CO" "CP" "CQ" "CR" "CS" "CT" "CU" "CV" "CW" "CX" "CY" "CZ"
#> [105] "DA" "DB" "DC" "DD" "DE" "DF" "DG" "DH" "DI" "DJ" "DK" "DL" "DM"
#> [118] "DN" "DO" "DP" "DQ" "DR" "DS" "DT" "DU" "DV" "DW" "DX" "DY" "DZ"
#> [131] "EA" "EB" "EC" "ED" "EE" "EF" "EG" "EH" "EI" "EJ" "EK" "EL" "EM"
#> [144] "EN" "EO" "EP" "EQ" "ER" "ES" "ET" "EU" "EV" "EW" "EX" "EY" "EZ"
#> [157] "FA" "FB" "FC" "FD" "FE" "FF" "FG" "FH" "FI" "FJ" "FK" "FL" "FM"
#> [170] "FN" "FO" "FP" "FQ" "FR" "FS" "FT" "FU" "FV" "FW" "FX" "FY" "FZ"
#> [183] "GA" "GB" "GC" "GD" "GE" "GF" "GG" "GH" "GI" "GJ" "GK" "GL" "GM"
#> [196] "GN" "GO" "GP" "GQ" "GR" "GS" "GT" "GU" "GV" "GW" "GX" "GY" "GZ"
#> [209] "HA" "HB" "HC" "HD" "HE" "HF" "HG" "HH" "HI" "HJ" "HK" "HL" "HM"
#> [222] "HN" "HO" "HP" "HQ" "HR" "HS" "HT" "HU" "HV" "HW" "HX" "HY" "HZ"
#> [235] "IA" "IB" "IC" "ID" "IE" "IF" "IG" "IH" "II" "IJ" "IK" "IL" "IM"
#> [248] "IN" "IO" "IP" "IQ" "IR" "IS" "IT" "IU" "IV" "IW" "IX" "IY" "IZ"
#> [261] "JA" "JB" "JC" "JD" "JE" "JF" "JG" "JH" "JI" "JJ" "JK" "JL" "JM"
#> [274] "JN" "JO" "JP" "JQ" "JR" "JS" "JT" "JU" "JV" "JW" "JX" "JY" "JZ"
#> [287] "KA" "KB" "KC" "KD" "KE" "KF" "KG" "KH" "KI" "KJ" "KK" "KL" "KM"
#> [300] "KN" "KO"
要将其写入 DF
中的第一行,您可以这样做:
DF <- rbind(c(LETTERS, c(sapply(LETTERS, paste0, LETTERS)))[1:301], DF)
我有一个包含 301 列的数据集。我想添加一行,其中包含一个以 a 开头的唯一字母并重复直到最后一列。我有的是:
DF:
Var1 Var2.....Var78 Var79...Var130V Var131
我想要的额外行是:
DF:
Var1 Var2.....Var78 Var79...Var130V Var131
NR A B CA CB EA EB
我试过了
DF[[nrow(DF)+1,]<- rep(letters)
DF[nrow(DF)+1,]<- paste(letters:ncol)
如有任何帮助,我们将不胜感激!
您可以使用此 one-liner:
在“Excel”列中获得相同的唯一字母 headersc(LETTERS, c(sapply(LETTERS, paste0, LETTERS)))[1:301]
#> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
#> [14] "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
#> [27] "AA" "AB" "AC" "AD" "AE" "AF" "AG" "AH" "AI" "AJ" "AK" "AL" "AM"
#> [40] "AN" "AO" "AP" "AQ" "AR" "AS" "AT" "AU" "AV" "AW" "AX" "AY" "AZ"
#> [53] "BA" "BB" "BC" "BD" "BE" "BF" "BG" "BH" "BI" "BJ" "BK" "BL" "BM"
#> [66] "BN" "BO" "BP" "BQ" "BR" "BS" "BT" "BU" "BV" "BW" "BX" "BY" "BZ"
#> [79] "CA" "CB" "CC" "CD" "CE" "CF" "CG" "CH" "CI" "CJ" "CK" "CL" "CM"
#> [92] "CN" "CO" "CP" "CQ" "CR" "CS" "CT" "CU" "CV" "CW" "CX" "CY" "CZ"
#> [105] "DA" "DB" "DC" "DD" "DE" "DF" "DG" "DH" "DI" "DJ" "DK" "DL" "DM"
#> [118] "DN" "DO" "DP" "DQ" "DR" "DS" "DT" "DU" "DV" "DW" "DX" "DY" "DZ"
#> [131] "EA" "EB" "EC" "ED" "EE" "EF" "EG" "EH" "EI" "EJ" "EK" "EL" "EM"
#> [144] "EN" "EO" "EP" "EQ" "ER" "ES" "ET" "EU" "EV" "EW" "EX" "EY" "EZ"
#> [157] "FA" "FB" "FC" "FD" "FE" "FF" "FG" "FH" "FI" "FJ" "FK" "FL" "FM"
#> [170] "FN" "FO" "FP" "FQ" "FR" "FS" "FT" "FU" "FV" "FW" "FX" "FY" "FZ"
#> [183] "GA" "GB" "GC" "GD" "GE" "GF" "GG" "GH" "GI" "GJ" "GK" "GL" "GM"
#> [196] "GN" "GO" "GP" "GQ" "GR" "GS" "GT" "GU" "GV" "GW" "GX" "GY" "GZ"
#> [209] "HA" "HB" "HC" "HD" "HE" "HF" "HG" "HH" "HI" "HJ" "HK" "HL" "HM"
#> [222] "HN" "HO" "HP" "HQ" "HR" "HS" "HT" "HU" "HV" "HW" "HX" "HY" "HZ"
#> [235] "IA" "IB" "IC" "ID" "IE" "IF" "IG" "IH" "II" "IJ" "IK" "IL" "IM"
#> [248] "IN" "IO" "IP" "IQ" "IR" "IS" "IT" "IU" "IV" "IW" "IX" "IY" "IZ"
#> [261] "JA" "JB" "JC" "JD" "JE" "JF" "JG" "JH" "JI" "JJ" "JK" "JL" "JM"
#> [274] "JN" "JO" "JP" "JQ" "JR" "JS" "JT" "JU" "JV" "JW" "JX" "JY" "JZ"
#> [287] "KA" "KB" "KC" "KD" "KE" "KF" "KG" "KH" "KI" "KJ" "KK" "KL" "KM"
#> [300] "KN" "KO"
要将其写入 DF
中的第一行,您可以这样做:
DF <- rbind(c(LETTERS, c(sapply(LETTERS, paste0, LETTERS)))[1:301], DF)