我想从数据框中删除一行并对结果列求和.我根据其内容知道要删除的行,但不知道它的行号.下面我将介绍三个例子,其中两个有效.使用-删除行仅在第一行要被删除的作品.这是为什么?
我的问题与此类似:如何删除R中数据帧的第一行? 但是,根据行号删除行.
# This works.
state = 'OH'
my.data = read.table(text = "
county y1990 y2000
cc NA 2
OH NA 10
bb NA 1
", sep = "", header = TRUE, na.strings = "NA", stringsAsFactors = FALSE)
my.colsums2 <- colSums(my.data[!(my.data$county == state), 2:ncol(my.data)], na.rm=TRUE)
my.colsums2
# y1990 y2000
# 0 3
# This works.
my.data = read.table(text = "
county y1990 y2000
OH NA 10
cc NA 2
bb NA 1
", sep = "", …Run Code Online (Sandbox Code Playgroud) 我想对数据集中的列对进行平均,而不是使用移动平均线。我想将列分成两组,并找到每对的平均值。
我提供了一个示例数据集、所需的结果以及返回所需结果的嵌套 for 循环。我只是认为可能有更好的方法。抱歉,如果我忽略了另一篇文章中的解决方案。我确实在这里搜索过,但我没有像平时那样勤奋地搜索互联网。感谢您的任何建议。
x = read.table(text = "
site yr1 yr2 yr3 yr4
1 2 4 6 8
2 10 20 30 40
3 5 NA 2 3
4 100 100 NA NA",
sep = "", header = TRUE)
x
desired.outcome = read.table(text = "
site ave12 ave34
1 3 7
2 15 35
3 5 2.5
4 100 NA",
sep = "", header = TRUE)
result <- matrix(NA, ncol=((ncol(x)/2)+1), nrow=nrow(x))
for(i in 1: ((ncol(x)-1)/2)) {
for(j in 1:nrow(x)) …Run Code Online (Sandbox Code Playgroud) 我想创建一个指标变量矩阵。我最初的想法是使用 model.matrix,这里也建议使用:Automatically expand an R factor into a collection of 1/0 indicator variables for each factor level
但是,如果一个因子只有一个级别,则 model.matrix 似乎不起作用。
这是一个示例数据集,其中包含三个级别的因子“区域”:
dat = read.table(text = "
reg1 reg2 reg3
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 0 1
0 0 1
0 0 1
0 0 1
", sep = "", header = TRUE)
# model.matrix works if …Run Code Online (Sandbox Code Playgroud) 我想在数据框中按行列表.我可以在以下示例中使用tablewithin 获得足够的结果apply:
df.1 <- read.table(text = '
state county city year1 year2 year3 year4 year5
1 2 4 0 0 0 1 2
2 5 3 10 20 10 NA 10
2 7 1 200 200 NA NA 200
3 1 1 NA NA NA NA NA
', na.strings = "NA", header=TRUE)
tdf <- t(df.1)
apply(tdf[4:nrow(tdf),1:nrow(df.1)], 2, function(x) {table(x, useNA = "ifany")})
Run Code Online (Sandbox Code Playgroud)
结果如下:
[[1]]
x
0 1 2
3 1 1
[[2]]
x
10 20 <NA> …Run Code Online (Sandbox Code Playgroud) 是否可以向数字添加或保留一个或多个前导零而不将结果转换为字符?我已经找到了将前导零的每个解决方案返回一个字符串,其中包括:paste,formatC,format,和sprintf.
例如,可以x是0123或00123等,而不是123仍然是数字?
x <- 0123
Run Code Online (Sandbox Code Playgroud)
编辑
这不是必要的.我刚刚玩了下面的代码,最后两行给出了错误的答案.我只是想,如果我能用数字格式得到前导零,那么获得正确答案会更容易.
a7 = c(1,1,1,0); b7=c(0,1,1,1); # 4
a77 = '1110' ; b77='0111' ; # 4
a777 = 1110 ; b777=0111 ; # 4
length(b7[(b7 %in% intersect(a7,b7))])
Run Code Online (Sandbox Code Playgroud)
R - 计数匹配一个字符串和另一个字符串之间的字符,没有替换
keyword <- unlist(strsplit(a77, ''))
text <- unlist(strsplit(b77, ''))
sum(!is.na(pmatch(keyword, text)))
ab7 <- read.fwf(file = textConnection(as.character(rbind(a777, b777))), widths = c(1,1,1,1), colClasses = rep("character", 2))
length(ab7[2,][(ab7[2,] %in% intersect(ab7[1,],ab7[2,]))])
Run Code Online (Sandbox Code Playgroud) 我想为美国和加拿大的网格单元着色。我的目标与这个问题非常相似:R Plot Filled Longitude-Latitude Grid Cells on Map 但是,该问题仅涉及美国,我无法弄清楚如何添加加拿大。
通过修改此处找到的代码,我能够绘制美国和加拿大的地图:https : //groups.google.com/forum/#!topic/ggplot2/KAKhoE0GO4U
library(ggplot2)
library(rgeos)
library(maps)
library(maptools)
PolygonCoords <- function(polygon) {
polygons <- polygon@Polygons
coords.list <- lapply(seq_along(polygons), function(i) {
# Extract the group, sequence, area, longitude, and latitude.
coords <- polygons[[i]]@coords
cbind(i, 1:nrow(coords), polygons[[i]]@area, coords)
})
coords.df <- as.data.frame(do.call(rbind, coords.list))
names(coords.df) <- c("order", "seq", "area", "long", "lat")
return(coords.df)
}
ConvertWorldSimple <- function(mapdata, min.area = 0) {
coords.list <- lapply(mapdata@polygons, PolygonCoords)
ncoords <- sapply(coords.list, nrow)
coords.df <- do.call(rbind, coords.list)
coords.df$country …Run Code Online (Sandbox Code Playgroud) 尽管阅读了文档,但我仍在努力理解函数参数在函数中的作用combn.
我有一个包含两列数据的表,对于每一列,我想计算该列中每个唯一数据对组合的比率.为简单起见,我们只关注一列:
V1
1 342.3
2 123.5
3 472.0
4 678.3
...
14 567.2
Run Code Online (Sandbox Code Playgroud)
我可以使用以下命令返回所有独特的组合:
combn(table[,1], 2)
Run Code Online (Sandbox Code Playgroud)
但当然这只会返回每对价值观.我想把它们分开以得到一个比例,但似乎无法弄清楚如何设置它.
据我所知,outer例如,你可以只提供运算符作为参数但是如何转移到combn?
combn(table[,1], 2, FUN = "/")
# obviously not correct
Run Code Online (Sandbox Code Playgroud) 我想在一个页面上放置两个正方形图,并使得到的图形填充一张纸的大部分.这似乎是一个非常基本的想法.但是,默认似乎是创建矩形图.当我指定绘图是正方形时,它们的大小相对于页面变得非常小.如果我成功地使方形图更大,则轴标签将被隐藏.我已经试过了无数的变化omi和mar与layout没有成功.
我想用base R.谢谢你的任何建议.
setwd('c:/users/markm/simple R programs')
x <- 1:10
y1.1 <- x ^2
y1.2 <- x + 50
y1.3 <- x ^1.5
pdf("plots_June1_2015.pdf")
par(mfrow=c(2,1))
plot(x, y1.1, type = 'l', col = 'black' , lwd = 1, lty = 1,
xlab = 'My X Axis',
ylab = 'My Y Axis')
lines(x, y1.2, type = 'l', col = 'black' , lwd = 1, lty = 2)
lines(x, y1.3, type = 'l', col = 'black' , …Run Code Online (Sandbox Code Playgroud) 我希望将相同的值分配给a中的多个变量data.frame.我看过这里似乎相似的多个帖子,但似乎没有解决我的具体问题.
这是data.frame我想要创建的示例:
data = data.frame(
a1 = 0.614, a2 = 0.614, a3 = 0.614, a4 = 0.614, a5 = 0.614,
a6 = 0.614, a7 = 0.614, a8 = 0.614, a9 = 0.614, a10 = 0.614,
c1 = -6.198, c2 = -6.198, c3 = -6.198, c4 = -6.198, c5 = -6.198,
c6 = -6.198, c7 = -6.198, c8 = -6.198, c9 = -6.198, c10 = -6.198,
d1 = 35.952, d2 = 35.952, d3 = 35.952, …Run Code Online (Sandbox Code Playgroud) 我试图将列表列表转换为data.frame.我意识到这个问题已被多次询问,但我找不到一个早期的答案在我的情况下有效.
这里有几个早期的帖子:
到目前为止,我看到的最好的答案是Benjamin Christoffersen在上面的第二个链接,但在我的情况下,我每个子列表只有一个值,我缺少观察,我的列表有名称,我希望保留.
这是我的示例数据集:
AA <- list(my.col1 = 1, my.col2 = 4, my.col3 = NULL, my.col4 = NULL)
BB <- list(my.col1 = NULL, my.col2 = NULL, my.col3 = NULL, my.col4 = NULL)
CC <- list(my.col1 = 13, my.col2 = 8, my.col3 = 2, my.col4 = 10)
DD <- list(my.col1 = NULL, my.col2 = NULL, my.col3 = -5, my.col4 = 7)
my.stuff <- list(AA, BB, CC, DD)
names(my.stuff) <- c("AA", "BB", "CC", "DD")
my.stuff
Run Code Online (Sandbox Code Playgroud)
这是所需的 …
r ×10
apply ×1
combinations ×1
dataframe ×1
dictionary ×1
ggplot2 ×1
indicator ×1
list ×1
matrix ×1
model.matrix ×1
plot ×1
r-grid ×1
sapply ×1