列を変更するには、列の文字列を変更します。 -- r フィールド と dataframe フィールド 関連 問題

Changing column to factor changes the string in the column












0
vote

問題

日本語

下記のデータフレームを持っています:

<事前> <コード> > print(baseline.ratio) Model Girder 1 Girder 2 Girder 3 Girder 4 Girder 5 1 UG-124 with 9' spacing 1.997 1.789 1.053 1.772 2.017 2 UG-116 with 9' spacing 1.945 1.809 1.060 1.786 1.947 3 UG-84 with 9' spacing 1.934 1.803 1.052 1.757 1.911 4 UG-100 with 9' spacing 1.925 1.826 1.067 1.792 1.913 5 UG-92 with 9' spacing 1.923 1.833 1.070 1.796 1.909 6 UG-108 with 9' spacing 1.908 1.804 1.059 1.776 1.910 7 UG-124 with 12' spacing 2.158 2.037 1.050 1.907 1.936 8 UG-116 with 12' spacing 2.137 2.025 1.044 1.892 1.916 9 UG-108 with 12' spacing 2.122 2.019 1.041 1.881 1.901 10 UG-92 with 12' spacing 2.114 2.012 1.034 1.870 1.888 11 UG-100 with 12' spacing 2.102 2.024 1.042 1.884 1.880 12 UG-84 with 12' spacing 2.099 1.992 1.021 1.850 1.870 13 UG-124 with 15' spacing 2.354 2.184 1.034 2.014 1.971 14 UG-116 with 15' spacing 2.343 2.170 1.030 2.001 1.952 15 UG-92 with 15' spacing 2.312 2.145 1.025 1.977 1.909 16 UG-100 with 15' spacing 2.310 2.144 1.024 1.976 1.912 17 UG-108 with 15' spacing 2.310 2.139 1.019 1.970 1.916 18 UG-84 with 15' spacing 2.296 2.123 1.017 1.955 1.884

モデル列に基づいてデータフレームを並べ替えたい。

<事前> <コード> c("UG-84 with 9' spacing","UG-84 with 12' spacing","UG-84 with 15' spacing", "UG-92 with 9' spacing","UG-92 with 12' spacing","UG-92 with 15' spacing", "UG-100 with 9' spacing","UG-100 with 12' spacing","UG-100 with 15' spacing", "UG-108 with 9' spacing","UG-108 with 12' spacing","UG-108 with 15' spacing", "UG-116 with 9' spacing","UG-116 with 12' spacing","UG-116 with 15' spacing", "UG-124 with 9' spacing","UG-124 with 12' spacing","UG-124 with 15' spacing")

要素として設定しようとすると、結果が変わります。

<事前> <コード> baseline.ratio$Model <- factor(baseline.ratio$Model, levels = c("UG-84 with 9' spacing","UG-84 with 12' spacing","UG-84 with 15' spacing", "UG-92 with 9' spacing","UG-92 with 12' spacing","UG-92 with 15' spacing", "UG-100 with 9' spacing","UG-100 with 12' spacing","UG-100 with 15' spacing", "UG-108 with 9' spacing","UG-108 with 12' spacing","UG-108 with 15' spacing", "UG-116 with 9' spacing","UG-116 with 12' spacing","UG-116 with 15' spacing", "UG-124 with 9' spacing","UG-124 with 12' spacing","UG-124 with 15' spacing")) baseline.ratio$Model <- baseline.ratio$Model[order(baseline.ratio$Model)]

間違ったデータフレームを与える:

<事前> <コード> > print(baseline.ratio) Model Girder 1 Girder 2 Girder 3 Girder 4 Girder 5 1 UG-84 with 9' spacing 1.997 1.789 1.053 1.772 2.017 2 UG-84 with 9' spacing 1.945 1.809 1.060 1.786 1.947 3 UG-84 with 12' spacing 1.934 1.803 1.052 1.757 1.911 4 UG-84 with 12' spacing 1.925 1.826 1.067 1.792 1.913 5 UG-84 with 15' spacing 1.923 1.833 1.070 1.796 1.909 6 UG-84 with 15' spacing 1.908 1.804 1.059 1.776 1.910 7 UG-92 with 9' spacing 2.158 2.037 1.050 1.907 1.936 8 UG-92 with 9' spacing 2.137 2.025 1.044 1.892 1.916 9 UG-92 with 12' spacing 2.122 2.019 1.041 1.881 1.901 10 UG-92 with 12' spacing 2.114 2.012 1.034 1.870 1.888 11 UG-92 with 15' spacing 2.102 2.024 1.042 1.884 1.880 12 UG-92 with 15' spacing 2.099 1.992 1.021 1.850 1.870 13 UG-100 with 9' spacing 2.354 2.184 1.034 2.014 1.971 14 UG-100 with 9' spacing 2.343 2.170 1.030 2.001 1.952 15 UG-100 with 12' spacing 2.312 2.145 1.025 1.977 1.909 16 UG-100 with 12' spacing 2.310 2.144 1.024 1.976 1.912 17 UG-100 with 15' spacing 2.310 2.139 1.019 1.970 1.916 18 UG-100 with 15' spacing 2.296 2.123 1.017 1.955 1.884

データフレームの列モデルで実際の文字の文字列を変更することなく、与えられたベクトルで定義されている順序でデータフレームの行を注文する方法はわかりません。

英語

I have the dataframe below:

> print(baseline.ratio)                      Model Girder 1 Girder 2 Girder 3 Girder 4 Girder 5 1   UG-124 with 9' spacing    1.997    1.789    1.053    1.772    2.017 2   UG-116 with 9' spacing    1.945    1.809    1.060    1.786    1.947 3    UG-84 with 9' spacing    1.934    1.803    1.052    1.757    1.911 4   UG-100 with 9' spacing    1.925    1.826    1.067    1.792    1.913 5    UG-92 with 9' spacing    1.923    1.833    1.070    1.796    1.909 6   UG-108 with 9' spacing    1.908    1.804    1.059    1.776    1.910 7  UG-124 with 12' spacing    2.158    2.037    1.050    1.907    1.936 8  UG-116 with 12' spacing    2.137    2.025    1.044    1.892    1.916 9  UG-108 with 12' spacing    2.122    2.019    1.041    1.881    1.901 10  UG-92 with 12' spacing    2.114    2.012    1.034    1.870    1.888 11 UG-100 with 12' spacing    2.102    2.024    1.042    1.884    1.880 12  UG-84 with 12' spacing    2.099    1.992    1.021    1.850    1.870 13 UG-124 with 15' spacing    2.354    2.184    1.034    2.014    1.971 14 UG-116 with 15' spacing    2.343    2.170    1.030    2.001    1.952 15  UG-92 with 15' spacing    2.312    2.145    1.025    1.977    1.909 16 UG-100 with 15' spacing    2.310    2.144    1.024    1.976    1.912 17 UG-108 with 15' spacing    2.310    2.139    1.019    1.970    1.916 18  UG-84 with 15' spacing    2.296    2.123    1.017    1.955    1.884 

I want to sort the dataframe based on the Model column in the order of :

c("UG-84 with 9' spacing","UG-84 with 12' spacing","UG-84 with 15' spacing",   "UG-92 with 9' spacing","UG-92 with 12' spacing","UG-92 with 15' spacing",   "UG-100 with 9' spacing","UG-100 with 12' spacing","UG-100 with 15' spacing",   "UG-108 with 9' spacing","UG-108 with 12' spacing","UG-108 with 15' spacing",   "UG-116 with 9' spacing","UG-116 with 12' spacing","UG-116 with 15' spacing",   "UG-124 with 9' spacing","UG-124 with 12' spacing","UG-124 with 15' spacing") 

When I try to set it as a factor, it changes the result:

baseline.ratio$Model <- factor(baseline.ratio$Model, levels =                                   c("UG-84 with 9' spacing","UG-84 with 12' spacing","UG-84 with 15' spacing",                                    "UG-92 with 9' spacing","UG-92 with 12' spacing","UG-92 with 15' spacing",                                    "UG-100 with 9' spacing","UG-100 with 12' spacing","UG-100 with 15' spacing",                                    "UG-108 with 9' spacing","UG-108 with 12' spacing","UG-108 with 15' spacing",                                    "UG-116 with 9' spacing","UG-116 with 12' spacing","UG-116 with 15' spacing",                                    "UG-124 with 9' spacing","UG-124 with 12' spacing","UG-124 with 15' spacing")) baseline.ratio$Model <- baseline.ratio$Model[order(baseline.ratio$Model)] 

Giving me the wrong dataframe:

> print(baseline.ratio)                      Model Girder 1 Girder 2 Girder 3 Girder 4 Girder 5 1    UG-84 with 9' spacing    1.997    1.789    1.053    1.772    2.017 2    UG-84 with 9' spacing    1.945    1.809    1.060    1.786    1.947 3   UG-84 with 12' spacing    1.934    1.803    1.052    1.757    1.911 4   UG-84 with 12' spacing    1.925    1.826    1.067    1.792    1.913 5   UG-84 with 15' spacing    1.923    1.833    1.070    1.796    1.909 6   UG-84 with 15' spacing    1.908    1.804    1.059    1.776    1.910 7    UG-92 with 9' spacing    2.158    2.037    1.050    1.907    1.936 8    UG-92 with 9' spacing    2.137    2.025    1.044    1.892    1.916 9   UG-92 with 12' spacing    2.122    2.019    1.041    1.881    1.901 10  UG-92 with 12' spacing    2.114    2.012    1.034    1.870    1.888 11  UG-92 with 15' spacing    2.102    2.024    1.042    1.884    1.880 12  UG-92 with 15' spacing    2.099    1.992    1.021    1.850    1.870 13  UG-100 with 9' spacing    2.354    2.184    1.034    2.014    1.971 14  UG-100 with 9' spacing    2.343    2.170    1.030    2.001    1.952 15 UG-100 with 12' spacing    2.312    2.145    1.025    1.977    1.909 16 UG-100 with 12' spacing    2.310    2.144    1.024    1.976    1.912 17 UG-100 with 15' spacing    2.310    2.139    1.019    1.970    1.916 18 UG-100 with 15' spacing    2.296    2.123    1.017    1.955    1.884 

I am not sure how to order the rows of the dataframe in the order that is defined by the given vector without changing the actual string of characters in column Model of the dataframe.

</div
     
   
   

回答リスト

1
 
vote
vote
ベストアンサー
 

あなたが試すべきだと思います

<事前> <コード> baseline.ratio <- baseline.ratio[order(baseline.ratio$Model),]
 

I guess you should try

baseline.ratio <- baseline.ratio[order(baseline.ratio$Model),] 
</div
 
 
1
 
vote

必要な順序でベクトルを作成することができます

<事前> <コード> vec <- c("UG-84 with 9' spacing","UG-84 with 12' spacing","UG-84 with 15' spacing", "UG-92 with 9' spacing","UG-92 with 12' spacing","UG-92 with 15' spacing", "UG-100 with 9' spacing","UG-100 with 12' spacing","UG-100 with 15' spacing", "UG-108 with 9' spacing","UG-108 with 12' spacing","UG-108 with 15' spacing", "UG-116 with 9' spacing","UG-116 with 12' spacing","UG-116 with 15' spacing", "UG-124 with 9' spacing","UG-124 with 12' spacing","UG-124 with 15' spacing")

使用<コード> items_data['col_def'] = items_data['col_a'][~items_data['col_a'].isin(items_data['col_b'])] 0 および<コード>および<コード> items_data['col_def'] = items_data['col_a'][~items_data['col_a'].isin(items_data['col_b'])] 1

。 <事前> <コード> items_data['col_def'] = items_data['col_a'][~items_data['col_a'].isin(items_data['col_b'])] 2
 

You could create a vector in the required order

vec <- c("UG-84 with 9' spacing","UG-84 with 12' spacing","UG-84 with 15' spacing",   "UG-92 with 9' spacing","UG-92 with 12' spacing","UG-92 with 15' spacing",   "UG-100 with 9' spacing","UG-100 with 12' spacing","UG-100 with 15' spacing",   "UG-108 with 9' spacing","UG-108 with 12' spacing","UG-108 with 15' spacing",   "UG-116 with 9' spacing","UG-116 with 12' spacing","UG-116 with 15' spacing",   "UG-124 with 9' spacing","UG-124 with 12' spacing","UG-124 with 15' spacing") 

The use match and order.

baseline.ratio <- baseline.ratio[order(match(baseline.ratio$Model, vec)), ] 
</div
 
 
1
 
vote

このような数値に順序付けされた要素を定式化してみてください:

<事前> <コード> items_data['col_def'] = items_data['col_a'][~items_data['col_a'].isin(items_data['col_b'])] 3

出力:

<事前> <コード> items_data['col_def'] = items_data['col_a'][~items_data['col_a'].isin(items_data['col_b'])] 4
 

Try formating the ordered factor to numeric like this:

#Values for order v1 <- c("UG-84 with 9' spacing","UG-84 with 12' spacing","UG-84 with 15' spacing",   "UG-92 with 9' spacing","UG-92 with 12' spacing","UG-92 with 15' spacing",   "UG-100 with 9' spacing","UG-100 with 12' spacing","UG-100 with 15' spacing",   "UG-108 with 9' spacing","UG-108 with 12' spacing","UG-108 with 15' spacing",   "UG-116 with 9' spacing","UG-116 with 12' spacing","UG-116 with 15' spacing",   "UG-124 with 9' spacing","UG-124 with 12' spacing","UG-124 with 15' spacing") #Create a factor for order df$Model2 <- factor(df$Model,levels = v1,ordered = T) #Order df2 <- df[order(as.numeric(df$Model2)),] df2$Model2 <- NULL 

Output:

df2                      Model Girder.1 Girder.2 Girder.3 Girder.4 Girder.5 3    UG-84 with 9' spacing    1.934    1.803    1.052    1.757    1.911 12  UG-84 with 12' spacing    2.099    1.992    1.021    1.850    1.870 18  UG-84 with 15' spacing    2.296    2.123    1.017    1.955    1.884 5    UG-92 with 9' spacing    1.923    1.833    1.070    1.796    1.909 10  UG-92 with 12' spacing    2.114    2.012    1.034    1.870    1.888 15  UG-92 with 15' spacing    2.312    2.145    1.025    1.977    1.909 4   UG-100 with 9' spacing    1.925    1.826    1.067    1.792    1.913 11 UG-100 with 12' spacing    2.102    2.024    1.042    1.884    1.880 16 UG-100 with 15' spacing    2.310    2.144    1.024    1.976    1.912 6   UG-108 with 9' spacing    1.908    1.804    1.059    1.776    1.910 9  UG-108 with 12' spacing    2.122    2.019    1.041    1.881    1.901 17 UG-108 with 15' spacing    2.310    2.139    1.019    1.970    1.916 2   UG-116 with 9' spacing    1.945    1.809    1.060    1.786    1.947 8  UG-116 with 12' spacing    2.137    2.025    1.044    1.892    1.916 14 UG-116 with 15' spacing    2.343    2.170    1.030    2.001    1.952 1   UG-124 with 9' spacing    1.997    1.789    1.053    1.772    2.017 7  UG-124 with 12' spacing    2.158    2.037    1.050    1.907    1.936 13 UG-124 with 15' spacing    2.354    2.184    1.034    2.014    1.971 
</div
 
 

関連する質問

1  タプルを鍵と値として辞書するPandas DataFrame  ( Pandas dataframe to dictionary with tuples as key and values ) 
私は以下の手助けが必要です: 私は以下のようにCSVファイルを持っていて、データフレーム 'DF'にロードされます。複数の領域、メモリ、VCPU、および各 '名前'に対応するストレージには異なる値があります。このデータフレームには1700行があります。 ...

12  Rの重複属性を持つ行を取り除きます  ( Get rid of rows with duplicate attributes in r ) 
私は次のような列を持つ大きなデータフレームを持っています。 <事前> <コード> ID, time, OS, IP そのデータフレームの各行は1つのエントリに対応します。一部の IDS 複数のエントリ(行)のデータフレーム内には存在します。それらの複数行を...

4  rbind()を使用して、複数のデータフレームを1つの大きさのデータに組み合わせる。  ( Using rbind to combine multiple data frames into one larger data frame within ) 
R-Studio 0.99.491とRバージョン3.2.3(2015-12-10)を使用しています。私はrの相対的なニュービーです、そして私はいくつかの助けに感謝します。私は、古いメディアサーバー上のサーバーログを使用しようとしているプロジェクトをやっていま...

1  データフレーム内のエントリの変更は、2つの値の間にあるか、値の下にあるかどうかに応じて  ( Changing entries in a dataframe depending on whether they are between two values ) 
私は1列の距離を持つデータフレームと他の列のスコア、例えば <事前> <コード> Distance Scores 1000. 1 1500. 1 ETC。 私は言う区分的な機能を持っています: 距離&gt = 1000の場合、スコアをゼロに変...

96  データフレームセル内のリストを別々の行に爆発させる方法  ( How to explode a list inside a dataframe cell into separate rows ) 
リストを含むPandasセルをそれらの値それぞれの行に変えることを目指しています。 だから、これを取ります: nearest_neighbors 列の値を解凍してスタックしたい場合は、各値が各 opponent indextでは行になるようにしてく...

1  Pandas Groupbyをグループ行グループに使用するには、列値を合計して決定できますか?  ( Can you use pandas groupby to group rows determined by summing a column value ) 
データフレームの行を1列の値でグループ化したいと思います。以下に示す例は、iが150の値で 'NumberOfTrades'の値をグループ化していた場合であるので、上位2行がグループ化され、下位3行がグループ化されます。私が理解している次のステップは、各グルー...

0  Pandasを使って最寄りの分をマージします  ( Merge with the nearest minute using pandas ) 
私は単に±1分の間隔で2つのデータフレームをマージしたいです。 ここにサンプルデータセットを持つname_df: <事前> <コード> Name Date A 2/19/2019 17:16:15 B 2/19/2019 17:...

1  データフレームの各列をフィルタリングすると、比類のない値の場合はNA  ( Filtering each column of a data frame an put na for unmatched values ) 
私は以下のような表を持っています: <事前> <コード> [,1] [,2] [,3] [,4] [,5] [1,] a A 0.06 0.31 -1.5 [2,] b B -0.75 0.2 ...

2  パンダの行を変更します  ( Change rows in pandas ) 
Pandasデータフレームにマトリックスを持っています <事前> <コード> print dfMatrix 0 1 2 3 4 0 10000 10 8 11 ...

1  R - コマンドライン引数によってデータフレーム名を決定したい  ( R i want data frame name to be determined by a command line argument ) 
コマンドライン引数でデータフレーム名を決定したいです。以下は、私がやろうとしていることを明確にするようにしてください...私は願っています! 使用: <事前> <コード> Context ctx = getApplicationContext(); S...




© 2022 cndgn.com All Rights Reserved. Q&Aハウス 全著作権所有