我想将一列分成两列。
当我在控制台下方添加代码时,表示该列已拆分。
> separate(refine, "Product code / number", into = c("product code", "number"), sep = "-")
# A tibble: 25 x 9
company `product code` number address city country
name
* <chr> <chr> <chr> <chr> <chr> <chr>
<chr>
1 philips p 5 Groningensingel 147 arnhem the netherlands
dhr p. jansen
2 philips p 43 Groningensingel 148 arnhem the netherlands
dhr p. hansen
3 philips x 3 Groningensingel 149 arnhem the netherlands
dhr j. Gansen
4 philips x 34 Groningensingel 150 arnhem the netherlands
dhr p. mansen
5 philips x 12 Groningensingel 151 arnhem the netherlands
dhr p. fransen
6 philips p 23 Groningensingel 152 arnhem the netherlands
dhr p. franssen
问题是,当我检查结果时,列没有拆分。
refine[,2]
# A tibble: 25 x 1
`Product code / number`
<chr>
1 p-5
2 p-43
3 x-3
4 x-34
答案 0 :(得分:0)
你永远不会在rstudio的脚本部分看到输出...或者在.R脚本文件中看到输出。该脚本仅存储命令。 输出可以在控制台中看到,它可以作为对象存储在环境中。在rstudio中,您可以通过命令View()可视化环境和某些类型的输出。 看看rstudio的备忘单:Rstudio.pdf
答案 1 :(得分:0)
You are not assigning the result. separate
doesn’t modify the existing variable refine
(few functions in R ever modify their arguments). It returns a new table with the split columns. You need to assign the result to a new (or existing) name:
result = separate(refine, "Product code / number", into = c("product code", "number"), sep = "-")
Instead of creating a new variable (result
), you can also overwrite refine
(refine = separate(refine, …)
) though I would generally recommend against modifying existing variables.
Contrary to what your question title states, there’s no difference in behaviour between R scripts and the R console. This is a fundamental, universal R behaviour.