Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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,我试图从数据帧中获取值,但当我试图获取单元格值时,R给出了一整列。我该如何解决这个问题 outer(row.names(temp_df),colnames(temp_df),function(x,y){paste(x,",",y,",",temp_df[x,y])}) # [,1] # [1,] "x , y , c(2, 1)" # [2,] "x2 , y , c(2, 1)" temp_df<-data.frame(c(1,2),c(3,4))

我试图从数据帧中获取值,但当我试图获取单元格值时,R给出了一整列。我该如何解决这个问题

outer(row.names(temp_df),colnames(temp_df),function(x,y){paste(x,",",y,",",temp_df[x,y])})
#      [,1]              
# [1,] "x , y , c(2, 1)" 
# [2,] "x2 , y , c(2, 1)"
temp_df<-data.frame(c(1,2),c(3,4))
temp_df<-data.frame(c(1,2),c(3,4))
colnames(temp_df)<-c("c1","c2")
row.names(temp_df)<-c("x","x2")
outer(row.names(temp_df),colnames(temp_df),function(x,y){paste(x,",",y,",",temp_df[x,y])})
#      [,1]                      [,2]                     
# [1,] "x , c1 , c(1, 2, 1, 2)"  "x , c2 , c(3, 4, 3, 4)" 
# [2,] "x2 , c1 , c(1, 2, 1, 2)" "x2 , c2 , c(3, 4, 3, 4)"
temp_df
#    c1 c2
# x   1  3
# x2  2  4

问题是,在您的例子中,x和y不是单个值,而是向量(本质上是行和列名称)。据我所知,然后您将尝试分割提供向量的temp_df,而不是单个值。 我可能不明白您想做什么,但我希望这有助于:

library(reshape2)
library(dplyr)

temp_df %>% mutate(name=rownames(temp_df)) %>% 
            melt(id.vars="name") %>%      
            apply(1,function(i){
                 return(paste(i,collapse=","))
            })

#"x,c1,1"  "x2,c1,2" "x,c2,3"  "x2,c2,4"
library(reshape2)
library(dplyr)

temp_df %>% mutate(name=rownames(temp_df)) %>% 
            melt(id.vars="name") %>%      
            apply(1,function(i){
                 return(paste(i,collapse=","))
            })

#"x,c1,1"  "x2,c1,2" "x,c2,3"  "x2,c2,4"