ID= c('A', 'A', 'A', 'B', 'B', 'B')
color=c('white', 'green', 'orange', 'white', 'green', 'green')
d = data.frame (ID, color)
Run Code Online (Sandbox Code Playgroud)
我想要的结果是
unique_colors=c(3,3,3,2,2,2)
d = data.frame (ID, color, unique_colors)
Run Code Online (Sandbox Code Playgroud)
或者在新的数据框架中更清楚c
ID= c('A','B')
unique_colors=c(3,2)
c = data.frame (ID,unique_colors)
Run Code Online (Sandbox Code Playgroud)
我试过的不同组合aggregate
和ave
以及by
和with
我想这是这些功能的组合.
解决方案包括:
length(unique(d$color))
Run Code Online (Sandbox Code Playgroud)
计算唯一元素的数量.
a <- c(rep(1:2,3))
b <- c("A","A","B","B","B","B")
df <- data.frame(a,b)
> str(b)
chr [1:6] "A" "A" "B" "B" "B" "B"
a b
1 1 A
2 2 A
3 1 B
4 2 B
5 1 B
6 2 B
Run Code Online (Sandbox Code Playgroud)
我想按变量分组a
并返回最常用的值b
我想要的结果看起来像
a b
1 1 B
2 2 B
Run Code Online (Sandbox Code Playgroud)
在dplyr
它会是这样的
df %>% group_by(a) %>% summarize (b = most.frequent(b))
Run Code Online (Sandbox Code Playgroud)
我dplyr
只提到可视化问题.
当我使用ggplot2
R包在Jupyter中创建绘图时,我得到一个指向"查看PDF"的图表的链接,而不是内联呈现的图表.
我知道传统上在IPython Notebook中你可以使用%matplotlib
魔术功能内联显示图表.Jupyter对R和ggplot2有类似的东西吗?
我需要做什么才能将图表内联显示为PDF链接?
我有一个数据框,我想绘制.我想将每一行绘制成一条线,但是当我这样做时,我的边距太大了.我试过做一个for循环,但我是R的新手,所以它不起作用.有人能帮帮我吗?
我的框架要大得多,它是67x135,但它看起来像这样:
V1 V2 V3 V4 V5
85 16 13 82 72
30 71 14 51 43
13 63 42 37 99
Run Code Online (Sandbox Code Playgroud) 我想删除所有包含值大于7
任意列中的单元格的行,无论是跨所有列还是跨特定列.
a <- c(3,6,99,7,8,9)
b <- c(99,6,3,4,5,6)
c <- c(2,5,6,7,8,3)
df <- data.frame (a,b,c)
a b c
1 3 99 2
2 6 6 5
3 99 3 6
4 7 4 7
5 8 5 8
6 9 6 3
Run Code Online (Sandbox Code Playgroud)
V1:我想删除包含大于7的值的所有行,而不管列是什么.
# result V1
a b c
2 6 6 5
4 7 4 7
Run Code Online (Sandbox Code Playgroud)
V2:我想删除b和c列中包含大于7的值的所有行
# result V2
a b c
2 6 6 5
3 99 3 6
4 7 4 7
6 9 6 …
Run Code Online (Sandbox Code Playgroud) 我想执行一个脚本file.R
使用Rscript
.在file.R
,我使用包dplyr
.
# file.R
df <- data.frame(ID,x,y,z,...)
library(dplyr)
filter(df, ID != "null")
......
Run Code Online (Sandbox Code Playgroud)
如果我没有在批处理文件中指定任何选项,则一切正常,因为file.R
包含该行library(dplyr)
# 1) no specification of packages in the batch file
Rscript.exe file.R arg1 arg2 arg3 > outputFile.Rout 2>&1
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加default-packages=utils
批处理文件,
# 2) specification of packages utils in the batch file
Rscript.exe default-packages=utils file.R arg1 arg2 arg3 > outputFile.Rout 2>&1
Run Code Online (Sandbox Code Playgroud)
部分file.R
使用dplyr
不工作了(Error in filter(df, ID != 'null') : Object 'ID' could not …
我想使用 R 将数据框中的特定行提取到新的数据框中。我有两列:City
和Household
。为了检测移动,我想要一个包含不在同一城市的家庭的新数据框。
例如,如果一个家庭出现 3 次,并且至少有一个城市与其他城市不同,我会保留它。否则,我删除这个家庭的 3 行。
City Household
Paris A
Paris A
Nice A
Limoge B
Limoge B
Toulouse C
Paris C
Run Code Online (Sandbox Code Playgroud)
在这里,我只想保留 HouseholdA
和 Household C
。
我有以下curl
要求:
curl --request GET --header "key: value" http://urlhere
Run Code Online (Sandbox Code Playgroud)
如何在 R 中运行请求?
例如,我想将数字转换167009345.8
为167009345.8
.
我用过很多方法,但都有问题。
例如,
x <- "167009345.8"
class(x) <- "numeric"`
Run Code Online (Sandbox Code Playgroud)
输出是167009346
.
但我想要十进制数167009345.8
。
我也用过as.numeric
,但也有同样的问题。
请你帮助我好吗?
我可以使用创建一系列单个字母
LETTERS[seq( from = 1, to = 10 )]
letters[seq( from = 1, to = 10 )]
Run Code Online (Sandbox Code Playgroud)我可以使用random
包创建一个不同长度的随机字符串
library(random)
string <- randomStrings(n=10, len=5, digits=TRUE, upperalpha=TRUE,
loweralpha=TRUE, unique=TRUE, check=TRUE)
Run Code Online (Sandbox Code Playgroud)不幸的是,我不能使用该set.seed
功能2
.
有没有办法在每次运行R文件时创建(唯一)字符串的相同(随机)组合?
我的结果看起来像这样(每次运行函数时都会得到相同的结果):
V1
[1,] "k7QET"
[2,] "CLlWm"
[3,] "yPuwh"
[4,] "JJqEX"
[5,] "38soF"
[6,] "xkozk"
[7,] "uaiOW"
[8,] "tZcrW"
[9,] "8K4Cc"
[10,] "RAhuU"
Run Code Online (Sandbox Code Playgroud)