add_column不向数据框添加列

时间:2021-12-08 02:29:49

I've used the add_column function from library(tibble) in my script, and it shows up fine in the console, but in the actual data frame df, it doesn't show that I've actually added any columns. The current structure of my data frame is a 60 x 17, but when I'm done adding my code shown below, it will end up being a 60 x 19, but when I use the following code, it doesn't give me any errors, and it still doesn't show the added columns.

我在我的脚本中使用了库(tibble)中的add_column函数,它在控制台中显示正常,但在实际的数据框df中,它并没有显示我实际上添加了任何列。我的数据框的当前结构是60 x 17,但是当我完成下面显示的代码添加后,它最终会是60 x 19,但是当我使用下面的代码时,它不会给我任何错误,它仍然没有显示添加的列。

add_column(df, 'Reading Depth'= extractdepth , .after = 1)
add_column(df, 'Half Depth'= halfdepth , .after = 2) 

Any idea on how to get both of my new column added to the data frame?

关于如何将我的新列添加到数据框中的任何想法?

1 个解决方案

#1


2  

as Nathan points out above; you need to either update your object or create a new object.

正如内森指出的那样;您需要更新对象或创建新对象。

Before you get to that you should however, always!, first load packages needed,

然而,在你到达之前,你应该总是!,首先加载所需的包,

# install.packages(c("tidyverse"), dependencies = TRUE)
library(tidyverse)

Second, also always, creating some date, here very inspired by ?add_column,

第二,也总是创造一些约会,这里的灵感来自于?add_column,

df <- tibble(x = 1:3, y = 3:1)

Third, almost always, show the data,

第三,几乎总是,显示数据,

df
#> # A tibble: 3 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3
#> 2     2     2
#> 3     3     1

great! Now to some solutions.

大!现在来一些解决方案。

Solution one, an option where we crate a new object,

解决方案一,我们创建一个新对象的选项,

df_new <- df %>% add_column(z = 1:3, w = 0)
df_new
#> # A tibble: 3 x 4
#>       x     y     z     w
#>   <int> <int> <int> <dbl>
#> 1     1     3     1     0
#> 2     2     2     2     0
#> 3     3     1     3     0

Solution two, a solution where df is updated,

解决方案二,更新df的解决方案,

df <- df %>% add_column(z = -1:1, w = 0)
df
#> # A tibble: 3 x 4
#>       x     y     z     w
#>   <int> <int> <int> <dbl>
#> 1     1     3    -1     0
#> 2     2     2     0     0
#> 3     3     1     1     0

Note that <- is used to write into or crate a new object – as Nathan's comment pointed out.

请注意,< - 用于写入或创建一个新对象 - 正如Nathan的评论指出的那样。

#1


2  

as Nathan points out above; you need to either update your object or create a new object.

正如内森指出的那样;您需要更新对象或创建新对象。

Before you get to that you should however, always!, first load packages needed,

然而,在你到达之前,你应该总是!,首先加载所需的包,

# install.packages(c("tidyverse"), dependencies = TRUE)
library(tidyverse)

Second, also always, creating some date, here very inspired by ?add_column,

第二,也总是创造一些约会,这里的灵感来自于?add_column,

df <- tibble(x = 1:3, y = 3:1)

Third, almost always, show the data,

第三,几乎总是,显示数据,

df
#> # A tibble: 3 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3
#> 2     2     2
#> 3     3     1

great! Now to some solutions.

大!现在来一些解决方案。

Solution one, an option where we crate a new object,

解决方案一,我们创建一个新对象的选项,

df_new <- df %>% add_column(z = 1:3, w = 0)
df_new
#> # A tibble: 3 x 4
#>       x     y     z     w
#>   <int> <int> <int> <dbl>
#> 1     1     3     1     0
#> 2     2     2     2     0
#> 3     3     1     3     0

Solution two, a solution where df is updated,

解决方案二,更新df的解决方案,

df <- df %>% add_column(z = -1:1, w = 0)
df
#> # A tibble: 3 x 4
#>       x     y     z     w
#>   <int> <int> <int> <dbl>
#> 1     1     3    -1     0
#> 2     2     2     0     0
#> 3     3     1     1     0

Note that <- is used to write into or crate a new object – as Nathan's comment pointed out.

请注意,< - 用于写入或创建一个新对象 - 正如Nathan的评论指出的那样。