2016-09-06 95 views
0

我正在接收來自JSON對象的輸出,但是JSON根據輸入返回三個字段,有時候是兩個somtimes之一。其結果是我有一個數據幀,其看起來像這樣:根據匹配標準將數據幀中的值從一列移動到另一列

 mixed  score  type 
1   1 0.0183232 positive 
2 neutral  <NA>  <NA> 
3 -0.566558 negative  <NA> 
4 0.473484 positive  <NA> 
5 0.856743 positive  <NA> 
6 -0.422655 negative  <NA> 

混合可以採用1個或0 分數的值可以取-1和+1 型之間的正或負的值可以採用的值或者正面,負面或中性

我不知道我怎麼能重新排列這些值在data.frame讓他們在正確的列即

 mixed  score  type 
1   1 0.018323 positive 
2  <NA> <NA>  neutral 
3  <NA> -0.566558 negative  
4  <NA> 0.473484 positive  
5  <NA> 0.856743 positive  
6  <NA> -0.422655 negative 
+0

如何從JSON對象創建data.frame?你看過[jsonlite](https://cran.r-project.org/web/packages/jsonlite/index.html)嗎? – Tutuchan

+0

根據輸入JSON將返回多達三個值,例如$ docSentiment 得分類型 「0.856743」「正面」或類似的東西$ docSentiment 類型 「中立」所以返回大多是非確定性的。輸出來自Alchemy API –

+0

@Tutochan是我使用jsonlite將輸出返回到一個對象:response_json_temp < - fromJSON(text_for_export $ Response) –

回答

0

不是一個完美的解決方案在所有,但最好我可以想出來。

### Seeds initial Dataframe 


mixed = c("1", "neutral", "0.473484", "-0.566558", "0.856743", "-0.422655", "-0.692675") 
score = c("0.0183232", "0", "positive", "negative", "positive", "negative", "negative") 
type = c("positive", "0", "0", "0", "0", "0", "0") 

df = data.frame(mixed, score, type) 


# Create a new DF (3 cols by nrow ize) for output 
df <- as.data.frame(matrix(0, ncol = 3, nrow = i)) 
setnames(df, old=c("V1","V2", "V3"), new=c("mixed", "score", "type")) 
df 

# Create a 2nd new DF (3 cols by nrow ize) for output 
df.2 <- as.data.frame(matrix(0, ncol = 3, nrow = i)) 
setnames(df.2, old=c("V1","V2", "V3"), new=c("mixed", "score", "type")) 
df.2 




#Check each column cell by cell if it does copy it do the shadow dataframe 
# Set all <NA> values to Null 
df[is.na(df)] <- 0 



# Set interation length to column length 
l <- 51 

# Checked the mixed column for '1' and then copy it to the new frame 
for(l in 1:l) 

    if (df$mixed[l] == '1') 
    { 
    df.2$mixed[l] <-df$mixed[l] 
    } 

# Checked the mixed column for a value which is less than 1 and then copy it to the score column in the new frame 

for(l in 1:l) 

    if (df$mixed[l] < '1') 
    { 
    df.2$score[l] <-df$mixed[l] 
    } 

# Checked the mixed column for positive/negative/neutral and then copy it to the type column in the new frame 

for(l in 1:l) 

    if (df$mixed[l] == "positive" | df$mixed[l] == "negative" | df$mixed[l] == "neutral") 
    { 
    df.2$type[l] <-df$mixed[l] 
    } 


# Checked the score column for '1' and then copy it to mixed column in the new frame 
for(l in 1:l) 

    if (df$score[l] == '1') 
    { 
    df.2$mixed[l] <-df$score[l] 
    } 

# Checked the score column for a value which is less than 1 and then copy it to the score column in the new frame 

for(l in 1:l) 

    if (df$score[l] < '1') 
    { 
    df.2$score[l] <-df$score[l] 
    } 

# Checked the score column for positive/negative/neutral and then copy it to the type column in the new frame 

for(l in 1:l) 

    if (df$score[l] == "positive" | df$score[l] == "negative" | df$score[l] == "neutral") 
    { 
    df.2$type[l] <-df$score[l] 
    } 



# Checked the type column for '1' and then copy it to mixed column in the new frame **This one works*** 
for(l in 1:l) 

    if (df$type[l] == '1') 
    { 
    df.2$mixed[l] <-df$type[l] 
    } 

# Checked the type column for a value which is less than 1 and then copy it to the score column in the new frame ** this one is erasing data in the new frame** 

for(l in 1:l) 

    if (df$type[l] < '1') 
    { 
    df.2$score[l] <-df$type[l] 
    } 

# Checked the type column for positive/negative/neutral and then copy it to the type column in the new frame **This one works*** 

for(l in 1:l) 

    if (df$type[l] == "positive" | df$type[l] == "negative" | df$type[l] == "neutral") 
    { 
    df.2$type[l] <-df$type[l] 
    } 
相關問題