我有一个这样的数据框:
A B Ind
1 10 8 1
2 9 10 2
3 7 1 2
4 19 20 1
5 . . .
Run Code Online (Sandbox Code Playgroud)
如何根据Ind值对列进行求和?如果Ind==1,则从列总和A,if Ind==2,列总和B.例如,前4行的输出应为10+10+1+19=30,其中前10行为A,第二行为10 B,第三行为1 B,第四行为19 A.
我有一个简短的问题,我想取消这个嵌套列表的嵌套:
mylist <- list(a = list(A=1, B=5),
b = list(C= 1, D = 2),
c = list(E = 1, F = 3))
Run Code Online (Sandbox Code Playgroud)
预期结果是:
> list(a=c(1, 5), b = c(1, 2), c = c(1, 3))
$a
[1] 1 5
$b
[1] 1 2
$c
[1] 1 3
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
时间
出于实现原因,在某些情况下,我有一个只包含一个列的data.frame
df=as.data.frame(alpha=1:15)
Run Code Online (Sandbox Code Playgroud)
如果我现在使用
df[-1, ]
Run Code Online (Sandbox Code Playgroud)
它返回一个向量,但我想将它保存为data.frame.有什么建议?
PS:列的名称包含信息,因此我必须保留它.
我正在使用R,并想知道是否有人可以帮助我.我想转换这个向量:
y<-c(1,1,1,1,2,2,3,3,3,4,4,4)
Run Code Online (Sandbox Code Playgroud)
至
y<-c(1,2,3,4,1,2,1,2,3,1,2,3)
Run Code Online (Sandbox Code Playgroud)
所以我可以做到以下几点:
v<-c(rep("a",4), rep("b",2), rep("c",3), rep("d",3))
paste (v, y, sep="")
[1] "a1" "a2" "a3" "a4" "b1" "b2" "c1" "c2" "c3" "d1" "d2" "d3"
Run Code Online (Sandbox Code Playgroud) HNY.正如主题中的问题所暗示的那样,我在尝试使用tseries包函数时遇到错误get.hist.quote().任何人都可以解释我的错误调用,或更改其签名/功能?
我昨天在工作中注意到这些错误.今天在我的家用机器上,同样的问题.符号,开始/结束日期和粒度(日与月)的各种组合的结果相同.
这是一个例子:
> spy = get.hist.quote(instrument= 'SPY',
start = "2000-01-01",
end = "2013-10-31",
quote="AdjClose",
provider = "yahoo",
origin="1970-01-01",
compression = "m",
retclass="zoo")
trying URL 'http://chart.yahoo.com/table.csv?s=SPY&a=0&b=01&c=2000&d=9&e=31&f=2013&g=m&q=q&y=0&z=SPY&x=.csv'
download error, retrying ...
trying URL 'http://chart.yahoo.com/table.csv?s=SPY&a=0&b=01&c=2000&d=9&e=31&f=2013&g=m&q=q&y=0&z=SPY&x=.csv'
download error, retrying ...
trying URL 'http://chart.yahoo.com/table.csv?s=SPY&a=0&b=01&c=2000&d=9&e=31&f=2013&g=m&q=q&y=0&z=SPY&x=.csv'
download error, retrying ...
trying URL 'http://chart.yahoo.com/table.csv?s=SPY&a=0&b=01&c=2000&d=9&e=31&f=2013&g=m&q=q&y=0&z=SPY&x=.csv'
download error, retrying ...
trying URL 'http://chart.yahoo.com/table.csv?s=SPY&a=0&b=01&c=2000&d=9&e=31&f=2013&g=m&q=q&y=0&z=SPY&x=.csv'
Error in get.hist.quote(instrument = "SPY", start = "2000-01-01", end = "2013-10-31", :
cannot open URL 'http://chart.yahoo.com/table.csv?s=SPY&a=0&b=01&c=2000&d=9&e=31&f=2013&g=m&q=q&y=0&z=SPY&x=.csv'
In addition: Warning messages:
1: In download.file(url, destfile, …Run Code Online (Sandbox Code Playgroud) 我需要添加换行符,但我正在努力使用atop函数expression().这main是我想要分成两行的部分.
> plot (DAC~Chlo,data=brazilw,
pch=15,col="red",cex=0.5,
main=expression("Fig. 3. Relationship
between diffuse attenuation coefficient at 490 nm
(K"[d]*") and chlorophyll concentration at three coral
reef sites"),
xlab=expression("Chlorophyll concentration (mg "*m^{-3}*")"),
cex.lab=0.8,
cex.main=0.8,
cex.axis=0.8,
font.main=1,
ylim=c(0,0.3),
xlim=c(0,3.5),
ylab=expression("K"[d]*"(m"*-1^{-1}*")"))
Run Code Online (Sandbox Code Playgroud) 我可以在整个美国地图中绘制特定州的数据.但我想只绘制状态图和数据(俄克拉荷马州).
我怎么能在R?
ggplot() +
geom_polygon( data=all_states, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
geom_point(data=stations,aes(x=long,y=lat),,colour="red",)+
ggtitle("Distribution of Flash Flood Events in CONUS")+
xlab('Longitude')+
ylab('Latitude')
dput(stations)
structure(list(coop = c(340017L, 340179L, 340256L, 340292L, 340548L,
340593L, 340908L, 341243L, 341504L, 341724L, 341828L, 342678L,
342912L, 342944L, 343497L, 343628L, 343821L, 343871L, 344055L,
344204L, 344235L, 344298L, 344573L, 344766L, 344861L, 345063L,
345509L, 345779L, 345855L, 346130L, 346139L, 346278L, 346629L,
346638L, 346670L, 346926L, 346935L, 347012L, 347254L, 348501L,
348677L, 349395L, 349422L, 349445L), lat = c(34.7864, 34.5903,
34.2208, 34.1714, 36.7683, 36.8125, 36.7236, 36.8003, 35.1756,
36.7747, 36.3225, …Run Code Online (Sandbox Code Playgroud) 假设我有以下代码
a<-c(1,2,3)
b<-'a'
Run Code Online (Sandbox Code Playgroud)
现在我将'b'中的字符串视为变量'a',当我将其输入到某个函数或操作中时.想象一下"treat.as.variable()"是一个真正的函数,它会这样做:
treat.as.variable(b)+c(1,2,4)
[1] 2 4 7
Run Code Online (Sandbox Code Playgroud)
是否有预定义的这样的功能?或者一般来说这样做的方法?
这有效:
df <- data.frame(a=c(1,2,3),b=c(4,2,3),c=c(2,5,1))
df %>% summarise(across( c(a,b,c), sum, na.rm=TRUE))
Run Code Online (Sandbox Code Playgroud)
但事实并非如此,因为 d 不存在:
df <- data.frame(a=c(1,2,3),b=c(4,2,3),c=c(2,5,1))
df %>% summarise(across( c(a,b,c,d), sum, na.rm=TRUE))
Run Code Online (Sandbox Code Playgroud)
这是函数的一部分,然后我使用 lapply 将其应用于列表中的一大堆数据帧,其中 d 几乎一直存在,所以我不能只删除 d。
我正在尝试实现这样的目标:
df <- data.frame(a=c(1,2,3),b=c(4,2,3),c=c(2,5,1))
df %>% summarise(across( any_of(c(a,b,c,d)), sum, na.rm=TRUE))
Run Code Online (Sandbox Code Playgroud) 我有一个这样的变量:
| 勇气 |
|---|
| 3.554,34 |
| 56,34 |
但它的类是“字符”,当我编码时:
gastosbolsonaro <- gastosbolsonaro %>% mutate(VALOR = as.numeric(VALOR))
Run Code Online (Sandbox Code Playgroud)
发生这样的事:
Problem while computing `VALOR = as.numeric(as.character(VALOR))`.
i NAs introduced by coercion
Run Code Online (Sandbox Code Playgroud)
所有值都变为 NA。
我想将超值货币更改为数字类别