Add columns in certain position to a set of data.frames contained in a list, preferably with mapply

huangapple go评论75阅读模式
英文:

Add columns in certain position to a set of data.frames contained in a list, preferably with mapply

问题

以下是翻译好的部分:

我有一个包含数据框的列表,我想在每个数据框的第4和第17位置添加两个空列,以便稍后与其他数据框合并它们。我需要按位置进行操作,因为列名不匹配。

df1 <- data.frame(x = 1:3, y=letters[1:3])
df2 <- data.frame(x = 4:6, y=letters[4:6])
data_list <- list(df1,df2)

我认为我可以使用类似于以下的代码作为基础,这段代码我从另一个问题中得到。

data_list2 <- mapply(`[<-`, data_list, 'emptyColumn', value = 'NA', SIMPLIFY = FALSE)

但是,我需要在特定位置添加列,比如在第2位。

此外,关于[<-是如何工作的解释将非常有帮助。

谢谢!

英文:

I have a set of data.frames on a list, and I would like to add two empty columns in the 4th and 17th position to each of the data frames, so I can later rbind them with others dfs. I need to do it using position because the names are not matching.

df1 &lt;- data.frame(x = 1:3, y=letters[1:3])
df2 &lt;- data.frame(x = 4:6, y=letters[4:6])
data_list &lt;- list(df1,df2)

I think I could use something like this as a base, which I took from another question.

data_list2 &lt;- mapply(`[&lt;-`, data_list, &#39;emptyColumn&#39;, value = &#39;NA&#39;, SIMPLIFY = FALSE)

But instead of just adding it at the end, I need to add it on a certain position, let's say on the 2nd, for example.

Also an explanation of what [&lt;- does would be great.

Thanks!

答案1

得分: 2

我提出了以下内容:

data_list2 <- mapply(add_column, data_list, 'emptyColumn'='NA',.after=4, SIMPLIFY = FALSE)

英文:

I came out with this:

data_list2 &lt;- mapply(add_column, data_list, &#39;emptyColumn&#39;=&#39;NA&#39;,.after=4, SIMPLIFY = FALSE)

答案2

得分: 1

你可以使用lapplyappend来实现类似这样的操作。只有当emptyColumn具有不同值向量时,mapply才有用。

lapply(data_list, \(x) data.frame(append(x, list("emptyColumn" = rep(NA, nrow(df1))), after = 1)))

[[1]]
x emptyColumn y
1 1 NA a
2 2 NA b
3 3 NA c

[[2]]
x emptyColumn y
1 4 NA d
2 5 NA e
3 6 NA f


<details>
<summary>英文:</summary>

You can do something like this with `lapply` and `append`. `mapply` will only be useful if you have different vectors of values for `emptyColumn`. 
```r
lapply(data_list, \(x) data.frame(append(x, list(&quot;emptyColumn&quot; = rep(NA, nrow(df1))), after = 1)))

[[1]]
  x emptyColumn y
1 1          NA a
2 2          NA b
3 3          NA c

[[2]]
  x emptyColumn y
1 4          NA d
2 5          NA e
3 6          NA f

答案3

得分: 1

可以的,这是代码的翻译部分:

Alternatively using `purrr` and `tibble::add_column()`:

df1 <- data.frame(x = 1:3, y=letters[1:3], z = 1:3, a = 1:3)
df2 <- data.frame(x = 4:6, y=letters[4:6], z = 1:3, a = 1:3)
data_list <- list(df1,df2)

library(purrr)
library(tibble)

positions <- c(4,1)
map2(data_list, positions, ~.x |>
       add_column(empty_column = NA, .before = .y))

Output:

[[1]]
  x y z empty_column a
1 1 a 1           NA 1
2 2 b 2           NA 2
3 3 c 3           NA 3

[[2]]
  empty_column x y z a
1           NA 4 d 1 1
2           NA 5 e 2 2
3           NA 6 f 3 3
英文:

Alternatively using purrr and tibble::add_column():

df1 &lt;- data.frame(x = 1:3, y=letters[1:3], z = 1:3, a = 1:3)
df2 &lt;- data.frame(x = 4:6, y=letters[4:6], z = 1:3, a = 1:3)
data_list &lt;- list(df1,df2)

library(purrr)
library(tibble)

positions &lt;- c(4,1)
map2(data_list, positions, ~.x |&gt; 
       add_column(empty_column = NA, .before = .y))

Output:

[[1]]
  x y z empty_column a
1 1 a 1           NA 1
2 2 b 2           NA 2
3 3 c 3           NA 3

[[2]]
  empty_column x y z a
1           NA 4 d 1 1
2           NA 5 e 2 2
3           NA 6 f 3 3

huangapple
  • 本文由 发表于 2023年2月27日 19:08:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定