英文:
Split a dataframe and past it back together
问题
我有一个数据框(如下所示的片段),我想要将单元格拆分,以便我可以为它们分配其他信息,然后将它们粘贴在一起。我遇到的问题是如何将它们拆分但保持每一行在一起。这是我所拥有的以及我试图做的一个示例:
当前数据框
newcolumn code name place
NA/NA 121/102 John/James GBR/GBR
NA/NA 100/103 Harry/Peter GBR/GBR
NA/NA 113/111 Will/Jamie GBR/GBR
NA/NA 109/112 Brian/Steve GBR/GBR
现在,我想将这个数据框分割成类似这样的内容:
newcolumn code name place
NA 121 John GBR
NA 102 James GBR
NA 100 Harry GBR
NA 103 Peter GBR
NA 113 Will GBR
NA 111 Jamie GBR
NA 109 Brian GBR
NA 112 Steve GBR
然后,在我填写了新列之后,我想能够将它们再次粘在一起(也许使用循环?),但这将使用第1和2行,第3和4行,依此类推。
英文:
I have a dataframe (snipet shown below) to which I want to split the cells so I can assign other information to them and then paste them back together. The issue im having is splitting them up but keeping each row together if that makes sense. Here an example of what i have and what im trying to do;
current df
newcolumn code name place
NA/NA 121/102 John/James GBR/GBR
NA/NA 100/103 Harry/Peter GBR/GBR
NA/NA 113/111 Will/Jamie GBR/GBR
NA/NA 109/112 Brian/Steve GBR/GBR
I now wish to seperate this df to something like this;
newcolumn code name place
NA 121 John GBR
NA 101 James GBR
NA 100 Harry GBR
NA 103 Peter GBR
NA 113 Will GBR
NA 111 Jamie GBR
NA 109 Brian GBR
NA 112 Steve GBR
Then after I have filled in my newcolumn I want to be able to past back together (maybe using a loop?) but this will be using row 1 and 2, 3 and 4 and so on
答案1
得分: 1
以下是您要翻译的内容:
第一部分:
A combination of `strsplit()` and `lapply()` gives the desired result for the 1st part:
df <- data.frame(
newcolumn = rep("NA/NA", 4),
code = c("121/102", "100/103", "113/111", "109/112"),
name = c("John/James", "Harry/Peter", "Will/Jamie",
"Brian/Steve"),
place = c("GBR/GBR", "GBR/GBR", "GBR/GBR", "GBR/GBR")
)
df
#> newcolumn code name place
#> 1 NA/NA 121/102 John/James GBR/GBR
#> 2 NA/NA 100/103 Harry/Peter GBR/GBR
#> 3 NA/NA 113/111 Will/Jamie GBR/GBR
#> 4 NA/NA 109/112 Brian/Steve GBR/GBR
df_split <-
as.data.frame(lapply(df, function(x) {
unlist(strsplit(x, split = "/", fixed = TRUE))
}))
df_split
#> newcolumn code name place
#> 1 NA 121 John GBR
#> 2 NA 102 James GBR
#> 3 NA 100 Harry GBR
#> 4 NA 103 Peter GBR
#> 5 NA 113 Will GBR
#> 6 NA 111 Jamie GBR
#> 7 NA 109 Brian GBR
#> 8 NA 112 Steve GBR
第二部分,结合使用 mapply()
,paste()
和选择交替行的 seq()
是一种选项:
df_split$newcolumn <- letters[seq_len(nrow(df_split))]
df_new <- mapply(paste,
df_split[seq(from = 1, to = nrow(df_split), by = 2), ],
df_split[seq(from = 2, to = nrow(df_split), by = 2), ],
SIMPLIFY = FALSE,
MoreArgs = list(sep = "/"))
df_new <- as.data.frame(df_new)
df_new
#> newcolumn code name place
#> 1 a/b 121/102 John/James GBR/GBR
#> 2 c/d 100/103 Harry/Peter GBR/GBR
#> 3 e/f 113/111 Will/Jamie GBR/GBR
#> 4 g/h 109/112 Brian/Steve GBR/GBR
<sup>Created on 2023-06-06 with reprex v2.0.2</sup>
英文:
A combination of strsplit()
and lapply()
gives the desired result for the 1st part:
df <- data.frame(
newcolumn = rep("NA/NA", 4),
code = c("121/102", "100/103", "113/111", "109/112"),
name = c("John/James", "Harry/Peter", "Will/Jamie",
"Brian/Steve"),
place = c("GBR/GBR", "GBR/GBR", "GBR/GBR", "GBR/GBR")
)
df
#> newcolumn code name place
#> 1 NA/NA 121/102 John/James GBR/GBR
#> 2 NA/NA 100/103 Harry/Peter GBR/GBR
#> 3 NA/NA 113/111 Will/Jamie GBR/GBR
#> 4 NA/NA 109/112 Brian/Steve GBR/GBR
df_split <-
as.data.frame(lapply(df, function(x) {
unlist(strsplit(x, split = "/", fixed = TRUE))
}))
df_split
#> newcolumn code name place
#> 1 NA 121 John GBR
#> 2 NA 102 James GBR
#> 3 NA 100 Harry GBR
#> 4 NA 103 Peter GBR
#> 5 NA 113 Will GBR
#> 6 NA 111 Jamie GBR
#> 7 NA 109 Brian GBR
#> 8 NA 112 Steve GBR
For the second part, a combination of mapply()
, paste()
and selecting alternating rows with seq()
is one option:
df_split$newcolumn <- letters[seq_len(nrow(df_split))]
df_new <- mapply(paste,
df_split[seq(from = 1, to = nrow(df_split), by = 2), ],
df_split[seq(from = 2, to = nrow(df_split), by = 2), ],
SIMPLIFY = FALSE,
MoreArgs = list(sep = "/"))
df_new <- as.data.frame(df_new)
df_new
#> newcolumn code name place
#> 1 a/b 121/102 John/James GBR/GBR
#> 2 c/d 100/103 Harry/Peter GBR/GBR
#> 3 e/f 113/111 Will/Jamie GBR/GBR
#> 4 g/h 109/112 Brian/Steve GBR/GBR
<sup>Created on 2023-06-06 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论