Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用其他列的标题在数据框中创建新列_R_Dataframe - Fatal编程技术网

R 使用其他列的标题在数据框中创建新列

R 使用其他列的标题在数据框中创建新列,r,dataframe,R,Dataframe,有一个数据框,看起来像这样 Forest Grass Shrub Water Binary 0.6 0.5 0.3 0.2 1 0.2 0.3 0.4 0.5 0 0.3 0.5 0.2 0.6 1 0.2 0.6 0.3 0.2 0 0.6 0.5 0.3 0.2 1 我想让R看看所有的行和 在一个新的柱子上写下数字最大的土地覆盖物的名称,这样我就有了这样一个表格 Forest Grass Shrub

有一个数据框,看起来像这样

Forest Grass Shrub Water Binary
0.6    0.5   0.3   0.2   1
0.2    0.3   0.4   0.5   0
0.3    0.5   0.2   0.6   1
0.2    0.6   0.3   0.2   0
0.6    0.5   0.3   0.2   1
我想让R看看所有的行和 在一个新的柱子上写下数字最大的土地覆盖物的名称,这样我就有了这样一个表格

Forest Grass Shrub Water Binary Most
0.6    0.5   0.3   0.2   1      Forest
0.2    0.3   0.4   0.5   0      Water
0.3    0.5   0.2   0.6   1      Water
0.2    0.6   0.3   0.2   0      Grass
0.6    0.5   0.3   0.2   1      Forest
然后我想让R看看二进制列,并计算组合forest-1forest-0,water-1,water-0出现的频率

不幸的是,我不知道如何做到这一点,并将感谢您的帮助

更新
library(data.table)
library(reshape2)
setDT(dt)
dt[,RowNo := .I]

dt2 <- melt(dt[,c(setdiff(colnames(dt),'Binary')), with = F], id.vars = c('RowNo'))
dt2 <- unique(dt2[, Most := as.character(variable)[which.max(value)], by = RowNo][,list(RowNo,Most)])

dt <- merge(dt2,dt, by = 'RowNo')

dt[,list(.N), by = list(Binary,Most)]
您也可以使用
max.col
,而不是
apply
,如下所示:

names(mydf)[max.col(mydf[-length(mydf)])]
# [1] "Forest" "Water"  "Water"  "Grass"  "Forest"
## Create your "Most" column. 
## This assumes "Binary" to be the last column. 
## Use names along with `setdiff`, index positions, or other approaches
##     if this is not the case with your actual data.
mydf$Most <- apply(mydf[-length(mydf)], 1, function(x) names(x)[which.max(x)])
mydf
#   Forest Grass Shrub Water Binary   Most
# 1    0.6   0.5   0.3   0.2      1 Forest
# 2    0.2   0.3   0.4   0.5      0  Water
# 3    0.3   0.5   0.2   0.6      1  Water
# 4    0.2   0.6   0.3   0.2      0  Grass
# 5    0.6   0.5   0.3   0.2      1 Forest

## This is the tabulation step.
## I'm assuming this is separate from the original data.frame
##     as it isn't shown to be as part of your desired output.
table(mydf[c("Most", "Binary")])
#         Binary
# Most     0 1
#   Forest 0 2
#   Grass  1 0
#   Water  1 1

原始答案 听上去,你可能在寻找这样的东西:

names(mydf)[max.col(mydf[-length(mydf)])]
# [1] "Forest" "Water"  "Water"  "Grass"  "Forest"
## Create your "Most" column. 
## This assumes "Binary" to be the last column. 
## Use names along with `setdiff`, index positions, or other approaches
##     if this is not the case with your actual data.
mydf$Most <- apply(mydf[-length(mydf)], 1, function(x) names(x)[which.max(x)])
mydf
#   Forest Grass Shrub Water Binary   Most
# 1    0.6   0.5   0.3   0.2      1 Forest
# 2    0.2   0.3   0.4   0.5      0  Water
# 3    0.3   0.5   0.2   0.6      1  Water
# 4    0.2   0.6   0.3   0.2      0  Grass
# 5    0.6   0.5   0.3   0.2      1 Forest

## This is the tabulation step.
## I'm assuming this is separate from the original data.frame
##     as it isn't shown to be as part of your desired output.
table(mydf[c("Most", "Binary")])
#         Binary
# Most     0 1
#   Forest 0 2
#   Grass  1 0
#   Water  1 1
##创建您的“Most”列。
##这假定“Binary”是最后一列。
##将名称与“setdiff”、索引位置或其他方法一起使用
##如果您的实际数据并非如此。

多年筹资框架$Most-1;显示您已尝试的内容R中有几个
name
-用于获取对象列名称的命令(几个命令是因为存在不同类型的对象),例如
names(your.data)
colnames(your.data)
col.names(your.data)
。一旦你在一个向量中有了列名,你可能就能够计算出剩下的。据我所知,在寻找最大数的列时,这也包括了二进制列,有没有办法忽略这个列?我认为
colClass
参数在这种情况下不起作用?你是对的。我做了编辑,现在应该可以了。另外,我不知道如何在这里使用
colClass
。我强烈建议学习和练习Coursera上的一些课程或任何在线教程中的R。我得到的输出是我想要的形式!然而,我得到的输出看起来非常符号化,这不可能是真的。你能发布你期望的输出吗?从结构上看,输出正是我想要的。它总结了所有1和0的类的出现。但是这些值太相似了,所有1的总和对于每个类大约为3000,对于0的总和对于每个类大约为400。我预计产出将更加“多样化/不均衡”。(我查看了美国的土地覆盖情况,以每0.5°Lat/lon最大的土地覆盖发生率为例,然后将每个单元格与1/0(单元格中有丛林火灾/单元格中没有丛林火灾))进行比较,希望你明白我的意思