R语言中在指定位置处插入列、或者行
1、任意位置插入列
> a <- letters[1:5] > b <- LETTERS[1:5] > c <- sample(1:10,5) > d <- sample(letters[1:10],5) > e <- data.frame(a, b, c, d) > e ## 测试data.frame a b c d 1 a A 5 f 2 b B 6 c 3 c C 1 j 4 d D 3 d 5 e E 4 e > xc <- 11:15 > cbind(xc, e) ## 在第一列前插入列 xc a b c d 1 11 a A 5 f 2 12 b B 6 c 3 13 c C 1 j 4 14 d D 3 d 5 15 e E 4 e > cbind(e, xc) ## 在最后一列后插入列 a b c d xc 1 a A 5 f 11 2 b B 6 c 12 3 c C 1 j 13 4 d D 3 d 14 5 e E 4 e 15 > cbind(e[,1:2], xc, e[,3:ncol(e)]) ## 在2、3列之间插入列 a b xc c d 1 a A 11 5 f 2 b B 12 6 c 3 c C 13 1 j 4 d D 14 3 d 5 e E 15 4 e
2、任意位置插入行
> a <- letters[1:5] > b <- LETTERS[1:5] > c <- sample(1:10,5) > d <- sample(letters[1:10],5) > e <- data.frame(a, b, c, d) > e a b c d 1 a A 1 j 2 b B 7 b 3 c C 2 i 4 d D 10 f 5 e E 6 g > xr <- 11:14 > xr [1] 11 12 13 14 > rbind(xr, e) ## 第一行前插入行 a b c d 1 11 12 13 14 2 a A 1 j 3 b B 7 b 4 c C 2 i 5 d D 10 f 6 e E 6 g > rbind(e, xr) ## 最后一列插入行 a b c d 1 a A 1 j 2 b B 7 b 3 c C 2 i 4 d D 10 f 5 e E 6 g 6 11 12 13 14 > rbind(e[1:3,], xr, e[4:nrow(e),]) ## 在3、4行之间插入行 a b c d 1 a A 1 j 2 b B 7 b 3 c C 2 i 4 11 12 13 14 41 d D 10 f 5 e E 6 g
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2021-05-01 python中字符串拼接的三种方法。
2021-05-01 python中字符串的格式化
2021-05-01 python中字符串的拼接
2021-05-01 -bash: /usr/bin/python: Too many levels of symbolic links 解决办法。
2021-05-01 python中拆分字符串
2021-05-01 python中替换字符串中特定字符
2021-05-01 python中返回字符串中指定字符的索引