问:
find_all with multiple expressions for tags that include both attribute values specified
I'm working to pull all tags from an HTML file with both the attribute values h5 and ls3 but I'm not sure how to format the search so it doesn't return all instances of h5 and all instances of ls3.

regex = re.compile('h5')
re2 = re.compile('ls3')

ly = []

for EachPart in soup.find_all("div", {"class": regex}):
    ly.append(EachPart)
```

<p>This is currently only searching for h5 (as expected), but how can I format the <code>find_all</code> search to search for <code>re2</code> along with <code>regex</code>? Effectively, below I need to be pulling the tags that have h5 and ls3 and not the ones that are exclusively h5</p>

<p>Example HTML Tags:</p>

```
total

£m

17. Website policy

```

答:
Use css selectors with AND syntax. Below specifies elements with both attributes. More efficient than find and regex.

```<code>items = [item.text for item in soup.select('.ls3.h5')]
</code>```

Body:
I'm working to pull all tags from an HTML file with both the attribute values h5 and ls3 but I'm not sure how to format the search so it doesn't return all instances of h5 and all instances of ls3.

regex = re.compile('h5')
re2 = re.compile('ls3')

ly = []

for EachPart in soup.find_all("div", {"class": regex}):
    ly.append(EachPart)

This is currently only searching for h5 (as expected), but how can I format the find_all search to search for re2 along with regex? Effectively, below I need to be pulling the tags that have h5 and ls3 and not the ones that are exclusively h5

Example HTML Tags:

total

£m

17. Website policy

Body:
Use css selectors with AND syntax. Below specifies elements with both attributes. More efficient than find and regex.

items = [item.text for item in soup.select('.ls3.h5')]


来源: stackexchange

问:
Changes in one dictionary affecting another dictionary
Initially, I 'dict' and 'dict2' which is a copy of 'dict'.
If I replace a Key in 'dict', the change is applied to dict2 too.
How can this be avoided? Below is some sample code.

```<code>Sub Main()
    Dim dict As New Dictionary(Of String, Object) From {{"Big", "Small"}, {"Hot", "Cold"}}
    Dim dict2 As New Dictionary(Of String, Object) : dict2 = dict

    If dict.ContainsKey("Hot") Then 'Only makes changes to dict
        dict.Add("Warm", dict("Hot").ToString)
        dict.Remove("Hot")
    End If

    writeDict(dict) 'Displays said changes
    writeDict(dict2) 'Displays same changes as dict
End Sub

Sub writeDict(dict As Dictionary(Of String, Object)) 'Ignore this
    For Each i As KeyValuePair(Of String, Object) In dict
        Console.Write(i.ToString)
    Next
    Console.ReadLine()
End Sub
</code>```

Ideally I would pass in 'dict' to another Sub, validate it by replacing some keys, then exit the Sub.
Then I would resume working with the original dictionary.

But currently that's not working, because changes to dictionaries seem to be global.

答:
When you do "dict2 = dict" it doesn't create a new Dictionary, you just have two variables that points at the same place. You need to create a new instance and copy the data.</p>

```<code>    Dim dict As New Dictionary(Of String, Object) From {{"Big", "Small"}, {"Hot", "Cold"}}
    Dim dict2 As New Dictionary(Of String, Object)(dict)
</code>```

ContentLicense: CC BY-SA 4.0

Body:
Initially, I 'dict' and 'dict2' which is a copy of 'dict'.
If I replace a Key in 'dict', the change is applied to dict2 too.
How can this be avoided? Below is some sample code.

Sub Main()
    Dim dict As New Dictionary(Of String, Object) From {{"Big", "Small"}, {"Hot", "Cold"}}
    Dim dict2 As New Dictionary(Of String, Object) : dict2 = dict

    If dict.ContainsKey("Hot") Then 'Only makes changes to dict
        dict.Add("Warm", dict("Hot").ToString)
        dict.Remove("Hot")
    End If

    writeDict(dict) 'Displays said changes
    writeDict(dict2) 'Displays same changes as dict
End Sub

Sub writeDict(dict As Dictionary(Of String, Object)) 'Ignore this
    For Each i As KeyValuePair(Of String, Object) In dict
        Console.Write(i.ToString)
    Next
    Console.ReadLine()
End Sub

Ideally I would pass in 'dict' to another Sub, validate it by replacing some keys, then exit the Sub.
Then I would resume working with the original dictionary.

But currently that's not working, because changes to dictionaries seem to be global.

Body:
When you do "dict2 = dict" it doesn't create a new Dictionary, you just have two variables that points at the same place. You need to create a new instance and copy the data.

    Dim dict As New Dictionary(Of String, Object) From {{"Big", "Small"}, {"Hot", "Cold"}}
    Dim dict2 As New Dictionary(Of String, Object)(dict)


问:
How do I make my button which is fixed at the bottom of a layout scroll, once the recyclerview on top grows in size
I have a very basic yet difficult issue where I have a button fixed at the bottom of the view,  but below recyclerview container. Once the container size grows, I want the button to go below the recyclerview (scrollable) instead of being fixed at the bottom of the view.Is there a way to achieve this?

I have looked online for a couple of ideas but didn't find anything quite like it yet.

i am basically using

```<code> <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clickable="false">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cl_root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false">

        <LinearLayout
            android:id="@+id/activity_g"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:elevation="0dp">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    app:subtitleTextColor="@color/white"
                    app:titleTextAppearance="@style/Toolbar.TitleText"
                    app:titleTextColor="@color/white">

                    <TextView
                        android:id="@+id/nav_back_btn"
                        style="@style/Body1RegLeftBlack"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="@dimen/activity_horizontal_margin"
                        android:gravity="center_horizontal"
                        android:background="@drawable/ic_backarrow" />

                    <TextView
                        android:id="@+id/g_main_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="18dp"
                        android:layout_marginTop="15dp"
                        android:letterSpacing="0.01"
                        android:gravity="center_horizontal"
                        android:text="@string/g"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textColor="#de000000"
                        android:textSize="20sp"
                        android:textStyle="normal"

                        />

                </android.support.v7.widget.Toolbar>

            </android.support.design.widget.AppBarLayout>

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/info_scrollview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:id="@+id/infoContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    android:clickable="false"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="30dp">

                        <TextView
                            android:id="@+id/info_location"
                            style="@style/Headline2LeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/activity_horizontal_margin"
                            android:textAppearance="@style/TextAppearance.Text.Chronicle"
                            android:textSize="28sp"
                            tools:text="@string/five_thirty_fifth_ave" />

                        <TextView
                            style="@style/Body1RegRightRed"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBaseline="@+id/info_location"
                            android:layout_alignParentEnd="true"
                            android:layout_marginEnd="@dimen/activity_horizontal_margin"
                            android:layout_toEndOf="@+id/info_location"
                            android:text="@string/location_change"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:visibility="gone" />
                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:layout_marginTop="44dp"
                        android:gravity="center_vertical"
                        android:weightSum="2">

                        <TextView
                            style="@style/Body1RegLeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_start_date"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/g_start_date" />

                        <TextView
                            style="@style/Body1RegRightBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_start_time"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/g_start_time" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/registerUserSeparatorColor" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:gravity="center_vertical"
                        android:weightSum="2">

                        <TextView
                            style="@style/Body1RegLeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_end_date"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:visibility="invisible"
                            android:text="@string/g_end_date" />

                        <TextView
                            style="@style/Body1RegRightBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_end_time"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/g_end_time" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/registerUserSeparatorColor" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="28dp"
                        android:letterSpacing="0.01"
                        android:lineSpacingExtra="4sp"
                        android:paddingTop="30dp"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="normal"
                        android:text="@string/g" />

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/g_item_list"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:overScrollMode="never"
                        android:divider="@null"
                        tools:listitem="@layout/item_g"/>

                    <TextView
                        style="@style/Body1RegLeftRed"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16sp"
                        android:layout_marginTop="20dp"
                        android:id="@+id/add_g"
                        android:lineHeight="22sp"
                        android:text="@string/add_g"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="58dp"
                        android:letterSpacing="0.01"
                        android:lineSpacingExtra="4sp"
                        android:visibility="invisible"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="normal"
                        android:text="@string/arrival_notifications" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:visibility="gone"
                        android:id="@+id/text_message_container"
                        android:layout_marginStart="16dp">

                        <TextView
                            android:id="@+id/text_message"
                            style="@style/Body1RegLeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="11dp"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/text_message" />

                        <TextView
                            style="@style/Body2RegLeftGrey"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/text_message"
                            android:layout_marginTop="2dp"
                            android:id="@+id/tap_phone_number_text"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/tap_to_add_a_phone_number" />

                        <android.support.v7.widget.SwitchCompat
                            android:id="@+id/text_message_switch"
                            style="@style/Color1SwitchStyle"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:layout_marginEnd="16dp" />
                    </RelativeLayout>
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginStart="16dp"
                        android:visibility="gone"
                        android:layout_marginTop="11dp"
                        android:background="@color/settingsSeparatorLineColor" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16sp"
                        android:layout_marginTop="100dp"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textStyle="normal"

                        android:letterSpacing="0.07"
                        android:lineSpacingExtra="0sp"
                        android:layout_gravity="center"
                        android:layout_marginBottom="30dp"
                        android:id="@+id/submit_btn"
                        android:text="@string/submit_list"
                        />

                </LinearLayout>
            </android.support.v4.widget.NestedScrollView>
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>
</android.support.design.widget.CoordinatorLayout>
</code>```

The problem above is, the submit_list btn doesnt stay fixed, if the recyclerview grows but theres still enough space to spare in the view.

I want to make it such that the button stays fixed and doesnt scroll UNTIL the size of the recyclerview goes beyond a certain limit from the bottom(not visible on the screen anymore), after which the button scrolls relative to the last item of the recyclerview and not aligned to the bottom anymore. any ideas how to go about it?

To give visual examples:

As you can see, initial screen has submit button aligned to the bottom and the rest of the content in place. As I add more users:

It still stays at the bottom since the recyclerview items are still within screen
 now, aqs soon as i add one more item, causing the list to scroll up :

As seen I want it to go below the recyclerview instead of stay fixed at bottom of the screen (the above screenshots are from ios where it works), was wondering if we can do the same for android?

答:
This is a demo link

And a Gif link

```<code>public class WeirdStickyBottomActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private TextView mStickyView;
    private LinearLayoutManager mLayoutManager;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weird_sticky_bottom);

        mRecyclerView = findViewById(R.id.recyclerview);
        mStickyView = findViewById(R.id.sticky);
        Button mAddButton = findViewById(R.id.button_add);
        Button mDeleteButton = findViewById(R.id.button_delete);

        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mAdapter = new MyAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
        hideStickyLayoutIfNeed();

        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.addItems();
                hideStickyLayoutIfNeed();
            }
        });

        mDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.deleteItems();
                hideStickyLayoutIfNeed();
            }
        });

        mStickyView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(WeirdStickyBottomActivity.this, "Sticky Click", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void hideStickyLayoutIfNeed() {
        mRecyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                mRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
                int firstVisible = mLayoutManager.findFirstVisibleItemPosition();
                int lastCompletelyVisible = mLayoutManager.findLastCompletelyVisibleItemPosition();

                //adapter list size display beyond the screen
                if (firstVisible > 0 || lastCompletelyVisible < mAdapter.getItemCount() - 1) {
                    mAdapter.addStickyItem(true);
                    if (mStickyView.getVisibility() == View.VISIBLE) {
                        mStickyView.setVisibility(View.GONE);
                    }
                } else if (firstVisible == 0 && lastCompletelyVisible == mAdapter.getItemCount() - 1) {
                    if (mStickyView.getVisibility() != View.VISIBLE) {
                        mStickyView.setVisibility(View.VISIBLE);
                    }
                }
                mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1);
                return true;
            }
        });
    }

    private static class MyAdapter extends RecyclerView.Adapter<ItemHolder> {

        private int mStickyIndex = -1;

        static final int HOLDER_TYPE_NORMAL = 1;
        static final int HOLDER_TYPE_STICKY = 2;

        private List<String> dataList = new ArrayList<>();
        private Context mContext;

        MyAdapter(Context context) {
            this.mContext = context;
            addItems();
        }

        public void addItems() {
            boolean hasStickyItem = mStickyIndex > 0;
            if (hasStickyItem) {
                dataList.remove(mStickyIndex);
                mStickyIndex = -1;
            }
            int size = dataList.size();
            for (int i = size; i < size + 5; i++) {
                dataList.add("This is a item");
            }
            notifyDataSetChanged();
        }

        public void deleteItems() {
            int size = dataList.size();
            if (size < 6) {
                return;
            }
            boolean hasStickyItem = mStickyIndex > 0;
            if (hasStickyItem) {
                dataList.remove(mStickyIndex);
                mStickyIndex = -1;
            }
            if (size > size - 5) {
                dataList.subList(size - 5, size - 1).clear();
            }
            notifyDataSetChanged();
        }

        public void addStickyItem(boolean notify) {
            //did not add sticky item before
            if (mStickyIndex <= 0) {
                mStickyIndex = dataList.size();
                dataList.add(mContext.getResources().getString(R.string.sticky_layout));
                if (notify) {
                    notifyDataSetChanged();
                }
            }
        }

        @NonNull
        @Override
        public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return ItemHolder.create(parent, viewType == HOLDER_TYPE_STICKY);
        }

        @Override
        public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
            holder.bind(dataList.get(position));
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        @Override
        public int getItemViewType(int position) {

            if (position == mStickyIndex) {
                return HOLDER_TYPE_STICKY;
            }

            return HOLDER_TYPE_NORMAL;
        }
    }

    private static class ItemHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private boolean isSticky;

        private ItemHolder(@NonNull View itemView, boolean isSticky) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            this.isSticky = isSticky;
        }

        void bind(String text) {
           textView.setText(text);
            if (isSticky) {
                textView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(v.getContext(), "Sticky Click", Toast.LENGTH_SHORT).show();
                    }
                });
                textView.setBackgroundColor(textView.getResources().getColor(R.color.colorPrimary));
            } else {
                textView.setOnClickListener(null);
                textView.setBackgroundColor(Color.WHITE);
            }
        }

        static ItemHolder create(ViewGroup parent, boolean isSticky) {
            return new ItemHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false), isSticky);
        }
    }
}

</code>```

```<code><?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_behavior="android.support.design.widget.AppBarLayout$Behavior">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="Collapse">

            <android.support.v7.widget.Toolbar
                android:id="@+id/tb_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="@string/app_name">

                <Button
                    android:id="@+id/button_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_add" />

                <Button
                    android:id="@+id/button_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_delete" />

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/sticky_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/sticky"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:text="@string/sticky_layout" />

    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>
</code>```

Body:
I have a very basic yet difficult issue where I have a button fixed at the bottom of the view,  but below recyclerview container. Once the container size grows, I want the button to go below the recyclerview (scrollable) instead of being fixed at the bottom of the view.Is there a way to achieve this?

I have looked online for a couple of ideas but didn't find anything quite like it yet.

i am basically using

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clickable="false">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cl_root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false">

        <LinearLayout
            android:id="@+id/activity_g"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:elevation="0dp">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    app:subtitleTextColor="@color/white"
                    app:titleTextAppearance="@style/Toolbar.TitleText"
                    app:titleTextColor="@color/white">

                    <TextView
                        android:id="@+id/nav_back_btn"
                        style="@style/Body1RegLeftBlack"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="@dimen/activity_horizontal_margin"
                        android:gravity="center_horizontal"
                        android:background="@drawable/ic_backarrow" />

                    <TextView
                        android:id="@+id/g_main_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="18dp"
                        android:layout_marginTop="15dp"
                        android:letterSpacing="0.01"
                        android:gravity="center_horizontal"
                        android:text="@string/g"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textColor="#de000000"
                        android:textSize="20sp"
                        android:textStyle="normal"

                        />

                </android.support.v7.widget.Toolbar>

            </android.support.design.widget.AppBarLayout>

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/info_scrollview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:id="@+id/infoContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    android:clickable="false"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="30dp">

                        <TextView
                            android:id="@+id/info_location"
                            style="@style/Headline2LeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/activity_horizontal_margin"
                            android:textAppearance="@style/TextAppearance.Text.Chronicle"
                            android:textSize="28sp"
                            tools:text="@string/five_thirty_fifth_ave" />

                        <TextView
                            style="@style/Body1RegRightRed"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBaseline="@+id/info_location"
                            android:layout_alignParentEnd="true"
                            android:layout_marginEnd="@dimen/activity_horizontal_margin"
                            android:layout_toEndOf="@+id/info_location"
                            android:text="@string/location_change"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:visibility="gone" />
                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:layout_marginTop="44dp"
                        android:gravity="center_vertical"
                        android:weightSum="2">

                        <TextView
                            style="@style/Body1RegLeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_start_date"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/g_start_date" />

                        <TextView
                            style="@style/Body1RegRightBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_start_time"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/g_start_time" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/registerUserSeparatorColor" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:gravity="center_vertical"
                        android:weightSum="2">

                        <TextView
                            style="@style/Body1RegLeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_end_date"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:visibility="invisible"
                            android:text="@string/g_end_date" />

                        <TextView
                            style="@style/Body1RegRightBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="16dp"
                            android:layout_weight="1"
                            android:id="@+id/g_end_time"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/g_end_time" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/registerUserSeparatorColor" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="28dp"
                        android:letterSpacing="0.01"
                        android:lineSpacingExtra="4sp"
                        android:paddingTop="30dp"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="normal"
                        android:text="@string/g" />

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/g_item_list"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:overScrollMode="never"
                        android:divider="@null"
                        tools:listitem="@layout/item_g"/>

                    <TextView
                        style="@style/Body1RegLeftRed"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16sp"
                        android:layout_marginTop="20dp"
                        android:id="@+id/add_g"
                        android:lineHeight="22sp"
                        android:text="@string/add_g"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="58dp"
                        android:letterSpacing="0.01"
                        android:lineSpacingExtra="4sp"
                        android:visibility="invisible"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="normal"
                        android:text="@string/arrival_notifications" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:visibility="gone"
                        android:id="@+id/text_message_container"
                        android:layout_marginStart="16dp">

                        <TextView
                            android:id="@+id/text_message"
                            style="@style/Body1RegLeftBlack"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="11dp"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/text_message" />

                        <TextView
                            style="@style/Body2RegLeftGrey"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/text_message"
                            android:layout_marginTop="2dp"
                            android:id="@+id/tap_phone_number_text"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:text="@string/tap_to_add_a_phone_number" />

                        <android.support.v7.widget.SwitchCompat
                            android:id="@+id/text_message_switch"
                            style="@style/Color1SwitchStyle"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:layout_marginEnd="16dp" />
                    </RelativeLayout>
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginStart="16dp"
                        android:visibility="gone"
                        android:layout_marginTop="11dp"
                        android:background="@color/settingsSeparatorLineColor" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16sp"
                        android:layout_marginTop="100dp"
                        android:textAppearance="@style/TextAppearance.Text.Roboto.Medium"
                        android:textStyle="normal"

                        android:letterSpacing="0.07"
                        android:lineSpacingExtra="0sp"
                        android:layout_gravity="center"
                        android:layout_marginBottom="30dp"
                        android:id="@+id/submit_btn"
                        android:text="@string/submit_list"
                        />

                </LinearLayout>
            </android.support.v4.widget.NestedScrollView>
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>
</android.support.design.widget.CoordinatorLayout>

The problem above is, the submit_list btn doesnt stay fixed, if the recyclerview grows but theres still enough space to spare in the view.

I want to make it such that the button stays fixed and doesnt scroll UNTIL the size of the recyclerview goes beyond a certain limit from the bottom(not visible on the screen anymore), after which the button scrolls relative to the last item of the recyclerview and not aligned to the bottom anymore. any ideas how to go about it?

To give visual examples:

As you can see, initial screen has submit button aligned to the bottom and the rest of the content in place. As I add more users:

It still stays at the bottom since the recyclerview items are still within screen
 now, aqs soon as i add one more item, causing the list to scroll up :

As seen I want it to go below the recyclerview instead of stay fixed at bottom of the screen (the above screenshots are from ios where it works), was wondering if we can do the same for android?

Body:
This is a demo link

And a Gif link

public class WeirdStickyBottomActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private TextView mStickyView;
    private LinearLayoutManager mLayoutManager;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weird_sticky_bottom);

        mRecyclerView = findViewById(R.id.recyclerview);
        mStickyView = findViewById(R.id.sticky);
        Button mAddButton = findViewById(R.id.button_add);
        Button mDeleteButton = findViewById(R.id.button_delete);

        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mAdapter = new MyAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
        hideStickyLayoutIfNeed();

        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.addItems();
                hideStickyLayoutIfNeed();
            }
        });

        mDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.deleteItems();
                hideStickyLayoutIfNeed();
            }
        });

        mStickyView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(WeirdStickyBottomActivity.this, "Sticky Click", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void hideStickyLayoutIfNeed() {
        mRecyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                mRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
                int firstVisible = mLayoutManager.findFirstVisibleItemPosition();
                int lastCompletelyVisible = mLayoutManager.findLastCompletelyVisibleItemPosition();

                //adapter list size display beyond the screen
                if (firstVisible > 0 || lastCompletelyVisible < mAdapter.getItemCount() - 1) {
                    mAdapter.addStickyItem(true);
                    if (mStickyView.getVisibility() == View.VISIBLE) {
                        mStickyView.setVisibility(View.GONE);
                    }
                } else if (firstVisible == 0 && lastCompletelyVisible == mAdapter.getItemCount() - 1) {
                    if (mStickyView.getVisibility() != View.VISIBLE) {
                        mStickyView.setVisibility(View.VISIBLE);
                    }
                }
                mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1);
                return true;
            }
        });
    }

    private static class MyAdapter extends RecyclerView.Adapter<ItemHolder> {

        private int mStickyIndex = -1;

        static final int HOLDER_TYPE_NORMAL = 1;
        static final int HOLDER_TYPE_STICKY = 2;

        private List<String> dataList = new ArrayList<>();
        private Context mContext;

        MyAdapter(Context context) {
            this.mContext = context;
            addItems();
        }

        public void addItems() {
            boolean hasStickyItem = mStickyIndex > 0;
            if (hasStickyItem) {
                dataList.remove(mStickyIndex);
                mStickyIndex = -1;
            }
            int size = dataList.size();
            for (int i = size; i < size + 5; i++) {
                dataList.add("This is a item");
            }
            notifyDataSetChanged();
        }

        public void deleteItems() {
            int size = dataList.size();
            if (size < 6) {
                return;
            }
            boolean hasStickyItem = mStickyIndex > 0;
            if (hasStickyItem) {
                dataList.remove(mStickyIndex);
                mStickyIndex = -1;
            }
            if (size > size - 5) {
                dataList.subList(size - 5, size - 1).clear();
            }
            notifyDataSetChanged();
        }

        public void addStickyItem(boolean notify) {
            //did not add sticky item before
            if (mStickyIndex <= 0) {
                mStickyIndex = dataList.size();
                dataList.add(mContext.getResources().getString(R.string.sticky_layout));
                if (notify) {
                    notifyDataSetChanged();
                }
            }
        }

        @NonNull
        @Override
        public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return ItemHolder.create(parent, viewType == HOLDER_TYPE_STICKY);
        }

        @Override
        public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
            holder.bind(dataList.get(position));
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        @Override
        public int getItemViewType(int position) {

            if (position == mStickyIndex) {
                return HOLDER_TYPE_STICKY;
            }

            return HOLDER_TYPE_NORMAL;
        }
    }

    private static class ItemHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private boolean isSticky;

        private ItemHolder(@NonNull View itemView, boolean isSticky) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            this.isSticky = isSticky;
        }

        void bind(String text) {
           textView.setText(text);
            if (isSticky) {
                textView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(v.getContext(), "Sticky Click", Toast.LENGTH_SHORT).show();
                    }
                });
                textView.setBackgroundColor(textView.getResources().getColor(R.color.colorPrimary));
            } else {
                textView.setOnClickListener(null);
                textView.setBackgroundColor(Color.WHITE);
            }
        }

        static ItemHolder create(ViewGroup parent, boolean isSticky) {
            return new ItemHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false), isSticky);
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_behavior="android.support.design.widget.AppBarLayout$Behavior">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="Collapse">

            <android.support.v7.widget.Toolbar
                android:id="@+id/tb_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="@string/app_name">

                <Button
                    android:id="@+id/button_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_add" />

                <Button
                    android:id="@+id/button_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_delete" />

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/sticky_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/sticky"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:text="@string/sticky_layout" />

    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>


问:
~0 vs. !0 vs. 1 in C
I was reading some code today by someone I consider to be a reasonable programmer, and I noticed they used a =~0 to set a loop quit variable.

Is there any compelling reason to do this rather than simply quit = 1;?

I'm mostly just curious, before I go ahead and change it. Thanks!

Example:

```<code>while(!quit){
    ...;

    if(!strcmp(s, "q"))
        quit=~0;
}
</code>```

答:
~0 is usually -1, while !0 is defined to be 1.

Of course, !~0 and !!0 are both 0, so there is no compelling reason to use one or the other, aside from the fact that ~0 is non-idiomatic (meaning that people won't know what the heck you're doing).

ContentLicense: CC BY-SA 3.0

Title: ~0 vs. !0 vs. 1 in C

Body:
I was reading some code today by someone I consider to be a reasonable programmer, and I noticed they used a =~0 to set a loop quit variable.

Is there any compelling reason to do this rather than simply quit = 1;?

I'm mostly just curious, before I go ahead and change it. Thanks!

Example:

while(!quit){
    ...;

    if(!strcmp(s, "q"))
        quit=~0;
}


问:
How do I install fonts in the IBM Cloud nodejs runtime? (cloud foundry)
I use the html-pdf component to generate pdf reports in my nodejs app and in order to properly embed custom fonts in the pdf document, these fonts must be installed in the server, in this case, in the IBM Cloud nodejs runtime. How do I do that?</p>

答: As far as I understand, you are trying to generate pdf from static html files which uses fonts that are already installed on a system, making them dependable on the system's available fonts. Why don't you fix the fonts in css and embed them as base64 encoded fonts? I once used this website to create that: https://transfonter.org</p>


问:
How to fix cvxpy output of arrays?
I am trying to write a portfolio optimization problem using cvxpy.

```<code>initial_weights = [0.045, 0.035, 0.024, 0.028...]
rets = (np.log(data/data.shift(1))
w = cvx.Variable(38)
ret = np.sum(rets.mean()*w)*252
prob = cvx.Problem(cvx.Maximize(ret), [cvx.sum_entries(w)==1, w>0.02, w<0.06])
</code>```

result = prob.solve()
After solving, w.value is of the form [[0.02], [0.02], [0.04]...]

I need to add more constraints like abs(list_final_weights-w)>0.005, but that will be possible if w.value is of the form of array of values instead of array of arrays. How can I fix this error?

答:
cvxpy does element-wise operations for subtraction, as long as they're of the same shape. You could convert your flat array into the appropriate shape that the solver needs - In this case it's (N, 1) instead of a flat array of shape (N,).

```<code>list_final_weights = np.reshape(list_final_weights, [len(list_final_weights), 1])
</code>```

Then you should be able to do the subtraction for the constraint:

```<code>list_final_weights - w >= 0.005
</code>```

Also cvxpy does not allow hard > or < inequalities. Only <= and >=.
https://www.cvxpy.org/tutorial/intro/index.html#constraints

Title: How to fix cvxpy output of arrays?

Body:
I am trying to write a portfolio optimization problem using cvxpy.

initial_weights = [0.045, 0.035, 0.024, 0.028...]
rets = (np.log(data/data.shift(1))
w = cvx.Variable(38)
ret = np.sum(rets.mean()*w)*252
prob = cvx.Problem(cvx.Maximize(ret), [cvx.sum_entries(w)==1, w>0.02, w<0.06])

result = prob.solve()
After solving, w.value is of the form [[0.02], [0.02], [0.04]...]

I need to add more constraints like abs(list_final_weights-w)>0.005, but that will be possible if w.value is of the form of array of values instead of array of arrays. How can I fix this error?

Body:
cvxpy does element-wise operations for subtraction, as long as they're of the same shape. You could convert your flat array into the appropriate shape that the solver needs - In this case it's (N, 1) instead of a flat array of shape (N,).

list_final_weights = np.reshape(list_final_weights, [len(list_final_weights), 1])

Then you should be able to do the subtraction for the constraint:

list_final_weights - w >= 0.005

Also cvxpy does not allow hard > or < inequalities. Only <= and >=.
https://www.cvxpy.org/tutorial/intro/index.html#constraints


问:
Running a regression of nested tibbles
I have a nested tibble which looks like the following:

```<code># A tibble: 2 x 3
  SCORE     score1_rank       score2_rank
  <chr>     <list>            <list>
1 scr_rnk_1 <tibble [54 x 5]> <tibble [54 x 5]>
2 scr_rnk_2 <tibble [46 x 5]> <tibble [46 x 5]>
</code>```

I want to construct regressions for each of the 4 tibbles. I can expand out the data by the following and run regressions individually:

```<code>    sub_data1 <- nested_df$score1_rank[[1]]
    sub_data2 <- nested_df$score1_rank[[2]]

#Reression 1
    sub_data1 <- sub_data1[!is.na(sub_data1$Y), ]
    lm(Y ~ X1 + X2, data = sub_data1)
#Regression 2
    sub_data2 <- sub_data2[!is.na(sub_data2$Y), ]
    lm(Y ~ X1 + X2, data = sub_data2)
</code>```

However I would like to try to do this for the whole nested tibble.

i.e. I am trying to map the regression over the tibbles.

Data:

```<code>nested_df <- structure(list(SCORE = c("scr_rnk_1", "scr_rnk_2"), score1_rank = list(
    structure(list(time = c("July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2016_June_2017", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2018_June_2019",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2008_June_2009", "July_2008_June_2009", "July_2008_June_2009",
    "July_2017_June_2018", "July_2017_June_2018", "July_2017_June_2018",
    "July_2009_June_2010", "July_2009_June_2010", "July_2009_June_2010",
    "July_2019_June_2020"), score1 = c(0.878385627705134, 0.829149886628575,
    0.873633400824437, 0.873191548477804, 0.833360020840671,
    0.821514348879447, 0.93893179382238, 0.902566094498171, 0.832521540654393,
    0.904546026086165, 0.944312545893212, 0.90721438246816, 0.925563285777056,
    0.837735581176652, 0.898314100598163, 0.881156591451732,
    0.927432166201199, 0.810462622843289, 0.924966424794594,
    0.54982486102469, 0.632637353015548, 0.93598101241571, 0.748712668464033,
    0.887355002120062, 0.00606213355201044, 0.66570681669867,
    0.809662797719473, 0.80883896141453, 0.410059100270974, 0.45097086832185,
    0.855118540355703, 0.73792861592456, 0.582170697766921, 0.910913548399676,
    0.909192361557635, 0.61000565934628, 0.541242004262667, 0.847840909074889,
    0.838844407944549, 0.638014235742945, 0.948686837455938,
    0.569343264654849, 0.942357992461572, 0.956483422999484,
    0.716630105733463, 0.757677906984471, 0.840660131450953,
    0.944095864840561, 0.74291963665858, 0.944596570938035, 0.916460742106468,
    0.90890022256817, 0.895889262055934, 0.886515265060623),
        Y = c(-0.0392143242061138, 0.00517332553863525, 0.0475661605596542,
        -0.0140374358743429, -0.0235463473945856, 0.0460794232785702,
        0.0647838711738586, -0.0257589742541313, 0.0539961569011211,
        -0.170428335666656, 0.0925306528806686, 0.11557175219059,
        0.0496749319136143, -0.11405622959137, 0.0666666403412819,
        -0.0189777128398418, -0.00572755141183734, 0.0277173686772585,
        -0.0241545476019383, 0.0328245237469673, 0.223529428243637,
        0.0253662765026092, 0.0394621938467026, 0.0815821811556816,
        0.0597507022321224, -0.0132956989109516, 0.0609685145318508,
        0.0393742695450783, -0.00168346334248781, -0.000859459512867033,
        0.0345749147236347, NA, 0.0327170714735985, 0.144188165664673,
        0.0415891073644161, 0.0028026478830725, -0.0840985849499702,
        0.00914959330111742, 0.0197730101644993, -0.0929021015763283,
        0.0382972247898579, NA, 0.015947800129652, 0.0136986169964075,
        -0.139593943953514, 0.113736107945442, 0.0216289088129997,
        -0.209788918495178, 0.00545153254643083, 0.126438871026039,
        0.0538020096719265, 0.0774460881948471, 0.0651820451021194,
        NA), X1 = c(0.14, 5.52, 0.14, -3.29, 1.82, -1.17, 1.93,
        2.7, -1.44, -1.74, 5.91, -2.05, 2.72, 1.86, 2.28, 1.39,
        3.49, 4.47, -1.52, 4.47, 9.85, -0.68, -2.52, 5.46, -0.43,
        -0.43, 2.3, 0.56, -8.19, 0.87, 2.53, NA, 7.32, 6.92,
        6.92, -6.18, -3.91, -6.32, 0.45, -8.88, -0.44, NA, -0.44,
        -1.11, -8.54, 7.28, -6.53, 1.93, 1.93, 1.93, 6.24, 8.62,
        6.24, NA), X2 = c(-0.5, 2.22, -0.5, 2.93, -0.17, 1.42,
        -0.53, 0.78, 1.67, -0.05, -0.39, -1.08, 0.46, 0.37, -0.62,
        0.17, 0.18, -0.69, -0.42, -0.69, 1.48, 1.32, 0.21, 0.17,
        -0.76, -0.76, 1.19, -0.66, -2.51, -0.38, -2.56, NA, -2.36,
        1.33, 1.33, 1.16, -0.25, -2.16, 0.04, -0.53, -0.46, NA,
        -0.46, 0.23, 2.23, -1.27, -0.57, -0.61, -0.61, -0.61,
        -0.19, -1.37, -0.19, NA)), class = c("tbl_df", "tbl",
    "data.frame"), row.names = c(NA, -54L)), structure(list(time = c("July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2008_June_2009", "July_2008_June_2009",
    "July_2008_June_2009", "July_2017_June_2018", "July_2017_June_2018",
    "July_2017_June_2018", "July_2009_June_2010", "July_2009_June_2010"
    ), score1 = c(0.910630243821458, 0.887211746784698, 0.920092482844549,
    0.94450683954903, 0.886972163304589, 0.991052738161695, 0.981619567238222,
    0.977490375052585, 0.961036277360393, 0.985523653404714,
    0.948091565971217, 0.959812930740014, 0.936269500157121,
    0.948541666157695, 0.939675946745415, 0.995146212267317,
    0.944554298851532, 0.982930629437269, 0.963858517802992,
    0.92872841572452, 0.968099127001545, 0.945198156814004, 0.892947157198215,
    0.906930889247629, 0.957790348580216, 0.928122479697648,
    0.953267485671018, 0.963714595673124, 0.976914001156382,
    0.973623547932495, 0.962870831719229, 0.978333062077069,
    0.958765402277667, 0.959032891808224, 0.972965648015492,
    0.982760065777063, 0.957170836537733, 0.961880715763936,
    0.975885654717621, 0.924673632533321, 0.925318007280836,
    0.987246011368269, 0.98249943727474, 0.980272445641619, 0.978206000922261,
    0.929807352926533), Y = c(0.0737265646457672, 0.0278251487761736,
    0.201131358742714, 0.125700861215591, 0.0777644738554955,
    -0.0130416098982096, -0.0990565568208694, 0.0333333089947701,
    -0.031569954007864, 0.0422280319035053, -0.0111790159717202,
    -0.278726726770401, -0.139534845948219, -0.0800638571381569,
    0.23757965862751, -0.0746169164776802, 0.0465963147580624,
    0.0337920561432838, -0.0111621227115393, -0.0133928591385484,
    0.0778210312128067, -0.0821536555886269, 0.00643268134444952,
    NA, 0.152694001793861, 0.0409262739121914, 0.0360006913542747,
    -0.0233012177050114, -0.211209982633591, -0.11425743252039,
    -0.169167995452881, 0.0282719731330872, 0.161968618631363,
    -0.0525752492249012, 0.0127659253776074, -0.0466842725872993,
    -0.115001328289509, -0.00946897640824318, 0.114568591117859,
    0.2675521671772, -0.0196253582835197, 0.123595483601093,
    NA, 0.12380950897932, -0.0350765138864517, -0.16666667163372
    ), X1 = c(2.01, 0.14, 5.06, 5.52, 1.82, 2.7, -3.09, 1.65,
    0.5, 1.93, -1.17, 2.25, 1.86, -1.88, 9.85, -3.9, 3.94, 7.6,
    4.47, -2.52, 1.32, 2.78, 0.09, NA, 0.88, 2.53, 2.53, 7.32,
    1.13, -6.18, -6.32, -0.3, 7.32, -6.18, 4.93, -1.11, -9.2,
    -7.52, 11.42, 9.96, -0.26, 1.93, NA, 0.49, 8.62, 0.49), X2 = c(2.18,
    -0.5, -1.03, 2.22, -0.17, 0.78, -2.72, -2.19, 1.22, -0.53,
    1.42, -0.51, 0.37, -1.55, 1.48, -0.22, -0.02, 2.08, -0.69,
    0.21, -1.2, -0.32, 0.35, NA, -0.57, -2.56, -2.56, -2.36,
    -3.09, 1.16, -2.16, 1.75, -2.36, 1.16, -0.77, 0.23, -1.33,
    -0.63, 1.64, 1.63, 2.85, -0.61, NA, 1.88, -1.37, 3.81)), class = c("tbl_df",
    "tbl", "data.frame"), row.names = c(NA, -46L))), score2_rank = list(
    structure(list(time = c("July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2016_June_2017", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2018_June_2019",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2008_June_2009", "July_2008_June_2009", "July_2008_June_2009",
    "July_2017_June_2018", "July_2017_June_2018", "July_2017_June_2018",
    "July_2009_June_2010", "July_2009_June_2010", "July_2009_June_2010",
    "July_2019_June_2020"), score2 = c(0.573384803196917, 0.95560973004494,
    0.936151601862601, 0.940067094946625, 0.790149367637373,
    0.885023225824309, 0.956490411723667, 0.918534374861312,
    0.9660240615445, 0.961407533200788, 0.794743982673356, 0.926614681101157,
    0.924390324452674, 0.838697174839086, 0.548480558835933,
    0.928419789574611, 0.942229561212187, 0.808215644539813,
    0.89946853678008, 0.931010276978734, 0.780385177969094, 0.945728847589739,
    0.958939314931932, 0.101395325662518, 0.0547541695358364,
    0.757995973046388, 0.815555744982054, 0.947726570770333,
    0.589921893700343, 0.924114006154793, 0.164071857964122,
    0.946752193254218, 0.801515206601873, 0.709037475517904,
    0.730962189352849, 0.872901083488831, 0.958819700206169,
    0.951829945538551, 0.924000702901887, 0.963439907199707,
    0.94482417669742, 0.817381450384857, 0.977233364779766, 0.881676744287434,
    0.820839678297149, 0.449214983785051, 0.536396658733052,
    0.756705578897905, 0.904306523171427, 0.947974271863387,
    0.947487349720247, 0.95821125132286, 0.890792036806817, 0.983129670844182
    ), Y = c(-0.0392143242061138, 0.0475661605596542, 0.0278251487761736,
    -0.0235463473945856, 0.0777644738554955, 0.0333333089947701,
    0.0460794232785702, 0.0647838711738586, -0.0257589742541313,
    -0.170428335666656, 0.0925306528806686, 0.11557175219059,
    -0.278726726770401, -0.139534845948219, -0.11405622959137,
    0.0666666403412819, -0.00572755141183734, 0.0277173686772585,
    0.23757965862751, -0.0241545476019383, 0.0465963147580624,
    0.0253662765026092, 0.0394621938467026, 0.00643268134444952,
    0.0597507022321224, -0.0132956989109516, 0.0609685145318508,
    0.0393742695450783, -0.00168346334248781, 0.0345749147236347,
    NA, 0.0360006913542747, 0.0327170714735985, -0.0233012177050114,
    0.0028026478830725, -0.0840985849499702, 0.161968618631363,
    0.00914959330111742, 0.0197730101644993, -0.0466842725872993,
    -0.0929021015763283, 0.0382972247898579, 0.015947800129652,
    0.0136986169964075, -0.139593943953514, 0.113736107945442,
    0.0216289088129997, -0.209788918495178, 0.00545153254643083,
    0.12380950897932, 0.0538020096719265, 0.0774460881948471,
    -0.16666667163372, NA), X1 = c(0.14, 0.14, 0.14, 1.82, 1.82,
    1.65, -1.17, 1.93, 2.7, -1.74, 5.91, -2.05, 2.25, 1.86, 1.86,
    2.28, 3.49, 4.47, 9.85, -1.52, 3.94, -0.68, -2.52, 0.09,
    -0.43, -0.43, 2.3, 0.56, -8.19, 2.53, NA, 2.53, 7.32, 7.32,
    -6.18, -3.91, 7.32, -6.32, 0.45, -1.11, -8.88, -0.44, -0.44,
    -1.11, -8.54, 7.28, -6.53, 1.93, 1.93, 0.49, 6.24, 8.62,
    0.49, NA), X2 = c(-0.5, -0.5, -0.5, -0.17, -0.17, -2.19,
    1.42, -0.53, 0.78, -0.05, -0.39, -1.08, -0.51, 0.37, 0.37,
    -0.62, 0.18, -0.69, 1.48, -0.42, -0.02, 1.32, 0.21, 0.35,
    -0.76, -0.76, 1.19, -0.66, -2.51, -2.56, NA, -2.56, -2.36,
    -2.36, 1.16, -0.25, -2.36, -2.16, 0.04, 0.23, -0.53, -0.46,
    -0.46, 0.23, 2.23, -1.27, -0.57, -0.61, -0.61, 1.88, -0.19,
    -1.37, 3.81, NA)), class = c("tbl_df", "tbl", "data.frame"
    ), row.names = c(NA, -54L)), structure(list(time = c("July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2008_June_2009", "July_2008_June_2009",
    "July_2008_June_2009", "July_2017_June_2018", "July_2017_June_2018",
    "July_2017_June_2018", "July_2009_June_2010", "July_2009_June_2010"
    ), score2 = c(0.977777238266838, 0.994161535248162, 0.973746623206586,
    0.959737686390477, 0.960771840809366, 0.973573416279972,
    0.971473417619078, 0.994362749200424, 0.998832204612857,
    0.969953961861552, 0.974595202023975, 0.990460167618893,
    0.977938934839813, 0.933720130788891, 0.997555980989323,
    0.983534940461115, 0.961638641355128, 0.98302503175898, 0.955924205281728,
    0.960588460795172, 0.980272014323638, 0.99319344527155, 0.990396166187007,
    0.96928405964874, 0.958824291095735, 0.94735915935544, 0.956799713877734,
    0.974313477760366, 0.959422857050319, 0.970981339110875,
    0.986720965210939, 0.988119219123952, 0.987757971968369,
    0.998331238333002, 0.985606980938901, 0.996309951852897,
    0.978123949182993, 0.980322946112709, 0.870995840583191,
    0.99620925825849, 0.952471805464684, 0.967521340577839, 0.997358168481063,
    0.954089152398106, 0.99961257213601, 0.971649355774121),
        Y = c(0.00517332553863525, 0.0737265646457672, 0.201131358742714,
        -0.0140374358743429, 0.125700861215591, -0.0130416098982096,
        -0.0990565568208694, 0.0539961569011211, -0.031569954007864,
        0.0422280319035053, -0.0111790159717202, 0.0496749319136143,
        -0.0189777128398418, -0.0800638571381569, -0.0746169164776802,
        0.0328245237469673, 0.223529428243637, 0.0337920561432838,
        -0.0111621227115393, -0.0133928591385484, 0.0815821811556816,
        0.0778210312128067, -0.0821536555886269, NA, -0.000859459512867033,
        0.152694001793861, 0.0409262739121914, -0.211209982633591,
        0.144188165664673, 0.0415891073644161, -0.11425743252039,
        -0.169167995452881, 0.0282719731330872, -0.0525752492249012,
        0.0127659253776074, -0.115001328289509, -0.00946897640824318,
        NA, 0.114568591117859, 0.2675521671772, -0.0196253582835197,
        0.123595483601093, NA, 0.126438871026039, -0.0350765138864517,
        0.0651820451021194), X1 = c(5.52, 2.01, 5.06, -3.29,
        5.52, 2.7, -3.09, -1.44, 0.5, 1.93, -1.17, 2.72, 1.39,
        -1.88, -3.9, 4.47, 9.85, 7.6, 4.47, -2.52, 5.46, 1.32,
        2.78, NA, 0.87, 0.88, 2.53, 1.13, 6.92, 6.92, -6.18,
        -6.32, -0.3, -6.18, 4.93, -9.2, -7.52, NA, 11.42, 9.96,
        -0.26, 1.93, NA, 1.93, 8.62, 6.24), X2 = c(2.22, 2.18,
        -1.03, 2.93, 2.22, 0.78, -2.72, 1.67, 1.22, -0.53, 1.42,
        0.46, 0.17, -1.55, -0.22, -0.69, 1.48, 2.08, -0.69, 0.21,
        0.17, -1.2, -0.32, NA, -0.38, -0.57, -2.56, -3.09, 1.33,
        1.33, 1.16, -2.16, 1.75, 1.16, -0.77, -1.33, -0.63, NA,
        1.64, 1.63, 2.85, -0.61, NA, -0.61, -1.37, -0.19)), class = c("tbl_df",
    "tbl", "data.frame"), row.names = c(NA, -46L)))), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))
</code>```

答:
2 nested lapplys is what I would use to do it across the whole tibble:

```<code>#iterate across score1 and score2
lapply(df[-1], function(x) {

  #iterate within score1 and then score2 to run the regressions
  lapply(x, function(y) {
    sub_data1 <- y[!is.na(y$Y), ]
    lm(Y ~ X1 + X2, data = sub_data1)
  })

})
</code>```

Output (4 regressions):

```<code># $score1_rank
# $score1_rank[[1]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# 0.010491     0.008486    -0.002082
#
#
# $score1_rank[[2]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# -0.013118     0.013098     0.008622
#
#
#
# $score2_rank
# $score2_rank[[1]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# -0.003704     0.007486    -0.009675
#
#
# $score2_rank[[2]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# -0.002017     0.012093     0.014742
</code>```

Title: Running a regression of nested tibbles

Body:
I have a nested tibble which looks like the following:

# A tibble: 2 x 3
  SCORE     score1_rank       score2_rank
  <chr>     <list>            <list>
1 scr_rnk_1 <tibble [54 x 5]> <tibble [54 x 5]>
2 scr_rnk_2 <tibble [46 x 5]> <tibble [46 x 5]>

I want to construct regressions for each of the 4 tibbles. I can expand out the data by the following and run regressions individually:

    sub_data1 <- nested_df$score1_rank[[1]]
    sub_data2 <- nested_df$score1_rank[[2]]

#Reression 1
    sub_data1 <- sub_data1[!is.na(sub_data1$Y), ]
    lm(Y ~ X1 + X2, data = sub_data1)
#Regression 2
    sub_data2 <- sub_data2[!is.na(sub_data2$Y), ]
    lm(Y ~ X1 + X2, data = sub_data2)

However I would like to try to do this for the whole nested tibble.

i.e. I am trying to map the regression over the tibbles.

Data:

nested_df <- structure(list(SCORE = c("scr_rnk_1", "scr_rnk_2"), score1_rank = list(
    structure(list(time = c("July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2016_June_2017", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2018_June_2019",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2008_June_2009", "July_2008_June_2009", "July_2008_June_2009",
    "July_2017_June_2018", "July_2017_June_2018", "July_2017_June_2018",
    "July_2009_June_2010", "July_2009_June_2010", "July_2009_June_2010",
    "July_2019_June_2020"), score1 = c(0.878385627705134, 0.829149886628575,
    0.873633400824437, 0.873191548477804, 0.833360020840671,
    0.821514348879447, 0.93893179382238, 0.902566094498171, 0.832521540654393,
    0.904546026086165, 0.944312545893212, 0.90721438246816, 0.925563285777056,
    0.837735581176652, 0.898314100598163, 0.881156591451732,
    0.927432166201199, 0.810462622843289, 0.924966424794594,
    0.54982486102469, 0.632637353015548, 0.93598101241571, 0.748712668464033,
    0.887355002120062, 0.00606213355201044, 0.66570681669867,
    0.809662797719473, 0.80883896141453, 0.410059100270974, 0.45097086832185,
    0.855118540355703, 0.73792861592456, 0.582170697766921, 0.910913548399676,
    0.909192361557635, 0.61000565934628, 0.541242004262667, 0.847840909074889,
    0.838844407944549, 0.638014235742945, 0.948686837455938,
    0.569343264654849, 0.942357992461572, 0.956483422999484,
    0.716630105733463, 0.757677906984471, 0.840660131450953,
    0.944095864840561, 0.74291963665858, 0.944596570938035, 0.916460742106468,
    0.90890022256817, 0.895889262055934, 0.886515265060623),
        Y = c(-0.0392143242061138, 0.00517332553863525, 0.0475661605596542,
        -0.0140374358743429, -0.0235463473945856, 0.0460794232785702,
        0.0647838711738586, -0.0257589742541313, 0.0539961569011211,
        -0.170428335666656, 0.0925306528806686, 0.11557175219059,
        0.0496749319136143, -0.11405622959137, 0.0666666403412819,
        -0.0189777128398418, -0.00572755141183734, 0.0277173686772585,
        -0.0241545476019383, 0.0328245237469673, 0.223529428243637,
        0.0253662765026092, 0.0394621938467026, 0.0815821811556816,
        0.0597507022321224, -0.0132956989109516, 0.0609685145318508,
        0.0393742695450783, -0.00168346334248781, -0.000859459512867033,
        0.0345749147236347, NA, 0.0327170714735985, 0.144188165664673,
        0.0415891073644161, 0.0028026478830725, -0.0840985849499702,
        0.00914959330111742, 0.0197730101644993, -0.0929021015763283,
        0.0382972247898579, NA, 0.015947800129652, 0.0136986169964075,
        -0.139593943953514, 0.113736107945442, 0.0216289088129997,
        -0.209788918495178, 0.00545153254643083, 0.126438871026039,
        0.0538020096719265, 0.0774460881948471, 0.0651820451021194,
        NA), X1 = c(0.14, 5.52, 0.14, -3.29, 1.82, -1.17, 1.93,
        2.7, -1.44, -1.74, 5.91, -2.05, 2.72, 1.86, 2.28, 1.39,
        3.49, 4.47, -1.52, 4.47, 9.85, -0.68, -2.52, 5.46, -0.43,
        -0.43, 2.3, 0.56, -8.19, 0.87, 2.53, NA, 7.32, 6.92,
        6.92, -6.18, -3.91, -6.32, 0.45, -8.88, -0.44, NA, -0.44,
        -1.11, -8.54, 7.28, -6.53, 1.93, 1.93, 1.93, 6.24, 8.62,
        6.24, NA), X2 = c(-0.5, 2.22, -0.5, 2.93, -0.17, 1.42,
        -0.53, 0.78, 1.67, -0.05, -0.39, -1.08, 0.46, 0.37, -0.62,
        0.17, 0.18, -0.69, -0.42, -0.69, 1.48, 1.32, 0.21, 0.17,
        -0.76, -0.76, 1.19, -0.66, -2.51, -0.38, -2.56, NA, -2.36,
        1.33, 1.33, 1.16, -0.25, -2.16, 0.04, -0.53, -0.46, NA,
        -0.46, 0.23, 2.23, -1.27, -0.57, -0.61, -0.61, -0.61,
        -0.19, -1.37, -0.19, NA)), class = c("tbl_df", "tbl",
    "data.frame"), row.names = c(NA, -54L)), structure(list(time = c("July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2008_June_2009", "July_2008_June_2009",
    "July_2008_June_2009", "July_2017_June_2018", "July_2017_June_2018",
    "July_2017_June_2018", "July_2009_June_2010", "July_2009_June_2010"
    ), score1 = c(0.910630243821458, 0.887211746784698, 0.920092482844549,
    0.94450683954903, 0.886972163304589, 0.991052738161695, 0.981619567238222,
    0.977490375052585, 0.961036277360393, 0.985523653404714,
    0.948091565971217, 0.959812930740014, 0.936269500157121,
    0.948541666157695, 0.939675946745415, 0.995146212267317,
    0.944554298851532, 0.982930629437269, 0.963858517802992,
    0.92872841572452, 0.968099127001545, 0.945198156814004, 0.892947157198215,
    0.906930889247629, 0.957790348580216, 0.928122479697648,
    0.953267485671018, 0.963714595673124, 0.976914001156382,
    0.973623547932495, 0.962870831719229, 0.978333062077069,
    0.958765402277667, 0.959032891808224, 0.972965648015492,
    0.982760065777063, 0.957170836537733, 0.961880715763936,
    0.975885654717621, 0.924673632533321, 0.925318007280836,
    0.987246011368269, 0.98249943727474, 0.980272445641619, 0.978206000922261,
    0.929807352926533), Y = c(0.0737265646457672, 0.0278251487761736,
    0.201131358742714, 0.125700861215591, 0.0777644738554955,
    -0.0130416098982096, -0.0990565568208694, 0.0333333089947701,
    -0.031569954007864, 0.0422280319035053, -0.0111790159717202,
    -0.278726726770401, -0.139534845948219, -0.0800638571381569,
    0.23757965862751, -0.0746169164776802, 0.0465963147580624,
    0.0337920561432838, -0.0111621227115393, -0.0133928591385484,
    0.0778210312128067, -0.0821536555886269, 0.00643268134444952,
    NA, 0.152694001793861, 0.0409262739121914, 0.0360006913542747,
    -0.0233012177050114, -0.211209982633591, -0.11425743252039,
    -0.169167995452881, 0.0282719731330872, 0.161968618631363,
    -0.0525752492249012, 0.0127659253776074, -0.0466842725872993,
    -0.115001328289509, -0.00946897640824318, 0.114568591117859,
    0.2675521671772, -0.0196253582835197, 0.123595483601093,
    NA, 0.12380950897932, -0.0350765138864517, -0.16666667163372
    ), X1 = c(2.01, 0.14, 5.06, 5.52, 1.82, 2.7, -3.09, 1.65,
    0.5, 1.93, -1.17, 2.25, 1.86, -1.88, 9.85, -3.9, 3.94, 7.6,
    4.47, -2.52, 1.32, 2.78, 0.09, NA, 0.88, 2.53, 2.53, 7.32,
    1.13, -6.18, -6.32, -0.3, 7.32, -6.18, 4.93, -1.11, -9.2,
    -7.52, 11.42, 9.96, -0.26, 1.93, NA, 0.49, 8.62, 0.49), X2 = c(2.18,
    -0.5, -1.03, 2.22, -0.17, 0.78, -2.72, -2.19, 1.22, -0.53,
    1.42, -0.51, 0.37, -1.55, 1.48, -0.22, -0.02, 2.08, -0.69,
    0.21, -1.2, -0.32, 0.35, NA, -0.57, -2.56, -2.56, -2.36,
    -3.09, 1.16, -2.16, 1.75, -2.36, 1.16, -0.77, 0.23, -1.33,
    -0.63, 1.64, 1.63, 2.85, -0.61, NA, 1.88, -1.37, 3.81)), class = c("tbl_df",
    "tbl", "data.frame"), row.names = c(NA, -46L))), score2_rank = list(
    structure(list(time = c("July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2016_June_2017", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2018_June_2019",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2008_June_2009", "July_2008_June_2009", "July_2008_June_2009",
    "July_2017_June_2018", "July_2017_June_2018", "July_2017_June_2018",
    "July_2009_June_2010", "July_2009_June_2010", "July_2009_June_2010",
    "July_2019_June_2020"), score2 = c(0.573384803196917, 0.95560973004494,
    0.936151601862601, 0.940067094946625, 0.790149367637373,
    0.885023225824309, 0.956490411723667, 0.918534374861312,
    0.9660240615445, 0.961407533200788, 0.794743982673356, 0.926614681101157,
    0.924390324452674, 0.838697174839086, 0.548480558835933,
    0.928419789574611, 0.942229561212187, 0.808215644539813,
    0.89946853678008, 0.931010276978734, 0.780385177969094, 0.945728847589739,
    0.958939314931932, 0.101395325662518, 0.0547541695358364,
    0.757995973046388, 0.815555744982054, 0.947726570770333,
    0.589921893700343, 0.924114006154793, 0.164071857964122,
    0.946752193254218, 0.801515206601873, 0.709037475517904,
    0.730962189352849, 0.872901083488831, 0.958819700206169,
    0.951829945538551, 0.924000702901887, 0.963439907199707,
    0.94482417669742, 0.817381450384857, 0.977233364779766, 0.881676744287434,
    0.820839678297149, 0.449214983785051, 0.536396658733052,
    0.756705578897905, 0.904306523171427, 0.947974271863387,
    0.947487349720247, 0.95821125132286, 0.890792036806817, 0.983129670844182
    ), Y = c(-0.0392143242061138, 0.0475661605596542, 0.0278251487761736,
    -0.0235463473945856, 0.0777644738554955, 0.0333333089947701,
    0.0460794232785702, 0.0647838711738586, -0.0257589742541313,
    -0.170428335666656, 0.0925306528806686, 0.11557175219059,
    -0.278726726770401, -0.139534845948219, -0.11405622959137,
    0.0666666403412819, -0.00572755141183734, 0.0277173686772585,
    0.23757965862751, -0.0241545476019383, 0.0465963147580624,
    0.0253662765026092, 0.0394621938467026, 0.00643268134444952,
    0.0597507022321224, -0.0132956989109516, 0.0609685145318508,
    0.0393742695450783, -0.00168346334248781, 0.0345749147236347,
    NA, 0.0360006913542747, 0.0327170714735985, -0.0233012177050114,
    0.0028026478830725, -0.0840985849499702, 0.161968618631363,
    0.00914959330111742, 0.0197730101644993, -0.0466842725872993,
    -0.0929021015763283, 0.0382972247898579, 0.015947800129652,
    0.0136986169964075, -0.139593943953514, 0.113736107945442,
    0.0216289088129997, -0.209788918495178, 0.00545153254643083,
    0.12380950897932, 0.0538020096719265, 0.0774460881948471,
    -0.16666667163372, NA), X1 = c(0.14, 0.14, 0.14, 1.82, 1.82,
    1.65, -1.17, 1.93, 2.7, -1.74, 5.91, -2.05, 2.25, 1.86, 1.86,
    2.28, 3.49, 4.47, 9.85, -1.52, 3.94, -0.68, -2.52, 0.09,
    -0.43, -0.43, 2.3, 0.56, -8.19, 2.53, NA, 2.53, 7.32, 7.32,
    -6.18, -3.91, 7.32, -6.32, 0.45, -1.11, -8.88, -0.44, -0.44,
    -1.11, -8.54, 7.28, -6.53, 1.93, 1.93, 0.49, 6.24, 8.62,
    0.49, NA), X2 = c(-0.5, -0.5, -0.5, -0.17, -0.17, -2.19,
    1.42, -0.53, 0.78, -0.05, -0.39, -1.08, -0.51, 0.37, 0.37,
    -0.62, 0.18, -0.69, 1.48, -0.42, -0.02, 1.32, 0.21, 0.35,
    -0.76, -0.76, 1.19, -0.66, -2.51, -2.56, NA, -2.56, -2.36,
    -2.36, 1.16, -0.25, -2.36, -2.16, 0.04, 0.23, -0.53, -0.46,
    -0.46, 0.23, 2.23, -1.27, -0.57, -0.61, -0.61, 1.88, -0.19,
    -1.37, 3.81, NA)), class = c("tbl_df", "tbl", "data.frame"
    ), row.names = c(NA, -54L)), structure(list(time = c("July_2013_June_2014",
    "July_2013_June_2014", "July_2013_June_2014", "July_2013_June_2014",
    "July_2013_June_2014", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2014_June_2015", "July_2014_June_2015",
    "July_2014_June_2015", "July_2016_June_2017", "July_2016_June_2017",
    "July_2016_June_2017", "July_2010_June_2011", "July_2010_June_2011",
    "July_2010_June_2011", "July_2010_June_2011", "July_2010_June_2011",
    "July_2012_June_2013", "July_2012_June_2013", "July_2012_June_2013",
    "July_2012_June_2013", "July_2012_June_2013", "July_2018_June_2019",
    "July_2018_June_2019", "July_2018_June_2019", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2015_June_2016", "July_2015_June_2016", "July_2015_June_2016",
    "July_2011_June_2012", "July_2011_June_2012", "July_2011_June_2012",
    "July_2011_June_2012", "July_2008_June_2009", "July_2008_June_2009",
    "July_2008_June_2009", "July_2017_June_2018", "July_2017_June_2018",
    "July_2017_June_2018", "July_2009_June_2010", "July_2009_June_2010"
    ), score2 = c(0.977777238266838, 0.994161535248162, 0.973746623206586,
    0.959737686390477, 0.960771840809366, 0.973573416279972,
    0.971473417619078, 0.994362749200424, 0.998832204612857,
    0.969953961861552, 0.974595202023975, 0.990460167618893,
    0.977938934839813, 0.933720130788891, 0.997555980989323,
    0.983534940461115, 0.961638641355128, 0.98302503175898, 0.955924205281728,
    0.960588460795172, 0.980272014323638, 0.99319344527155, 0.990396166187007,
    0.96928405964874, 0.958824291095735, 0.94735915935544, 0.956799713877734,
    0.974313477760366, 0.959422857050319, 0.970981339110875,
    0.986720965210939, 0.988119219123952, 0.987757971968369,
    0.998331238333002, 0.985606980938901, 0.996309951852897,
    0.978123949182993, 0.980322946112709, 0.870995840583191,
    0.99620925825849, 0.952471805464684, 0.967521340577839, 0.997358168481063,
    0.954089152398106, 0.99961257213601, 0.971649355774121),
        Y = c(0.00517332553863525, 0.0737265646457672, 0.201131358742714,
        -0.0140374358743429, 0.125700861215591, -0.0130416098982096,
        -0.0990565568208694, 0.0539961569011211, -0.031569954007864,
        0.0422280319035053, -0.0111790159717202, 0.0496749319136143,
        -0.0189777128398418, -0.0800638571381569, -0.0746169164776802,
        0.0328245237469673, 0.223529428243637, 0.0337920561432838,
        -0.0111621227115393, -0.0133928591385484, 0.0815821811556816,
        0.0778210312128067, -0.0821536555886269, NA, -0.000859459512867033,
        0.152694001793861, 0.0409262739121914, -0.211209982633591,
        0.144188165664673, 0.0415891073644161, -0.11425743252039,
        -0.169167995452881, 0.0282719731330872, -0.0525752492249012,
        0.0127659253776074, -0.115001328289509, -0.00946897640824318,
        NA, 0.114568591117859, 0.2675521671772, -0.0196253582835197,
        0.123595483601093, NA, 0.126438871026039, -0.0350765138864517,
        0.0651820451021194), X1 = c(5.52, 2.01, 5.06, -3.29,
        5.52, 2.7, -3.09, -1.44, 0.5, 1.93, -1.17, 2.72, 1.39,
        -1.88, -3.9, 4.47, 9.85, 7.6, 4.47, -2.52, 5.46, 1.32,
        2.78, NA, 0.87, 0.88, 2.53, 1.13, 6.92, 6.92, -6.18,
        -6.32, -0.3, -6.18, 4.93, -9.2, -7.52, NA, 11.42, 9.96,
        -0.26, 1.93, NA, 1.93, 8.62, 6.24), X2 = c(2.22, 2.18,
        -1.03, 2.93, 2.22, 0.78, -2.72, 1.67, 1.22, -0.53, 1.42,
        0.46, 0.17, -1.55, -0.22, -0.69, 1.48, 2.08, -0.69, 0.21,
        0.17, -1.2, -0.32, NA, -0.38, -0.57, -2.56, -3.09, 1.33,
        1.33, 1.16, -2.16, 1.75, 1.16, -0.77, -1.33, -0.63, NA,
        1.64, 1.63, 2.85, -0.61, NA, -0.61, -1.37, -0.19)), class = c("tbl_df",
    "tbl", "data.frame"), row.names = c(NA, -46L)))), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))

Body:
2 nested lapplys is what I would use to do it across the whole tibble:

#iterate across score1 and score2
lapply(df[-1], function(x) {

  #iterate within score1 and then score2 to run the regressions
  lapply(x, function(y) {
    sub_data1 <- y[!is.na(y$Y), ]
    lm(Y ~ X1 + X2, data = sub_data1)
  })

})

Output (4 regressions):

# $score1_rank
# $score1_rank[[1]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# 0.010491     0.008486    -0.002082
#
#
# $score1_rank[[2]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# -0.013118     0.013098     0.008622
#
#
#
# $score2_rank
# $score2_rank[[1]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# -0.003704     0.007486    -0.009675
#
#
# $score2_rank[[2]]
#
# Call:
#   lm(formula = Y ~ X1 + X2, data = sub_data1)
#
# Coefficients:
#   (Intercept)           X1           X2
# -0.002017     0.012093     0.014742


问:
Is there a Swifty way to unwrap optionals with string interpolation?
I'm building a RESTful API query using values collected from the user. Some of those values might not be supplied, so I'm using optionals.  There are Double, Int and String values.  The code I'm using seems clunky.  Is there a cleaner, Swifty way to write this block of code?

'''Swift

```<code>let baseURL = "http......"

// Start with required parms
let format = "?format=\(parms.format)"
let endTime = "&endtime=\(parms.endDate)"

// now for optionals
let startTime = parms.startDate != nil ? "&starttime=\(parms.startDate!)" : ""

// Sorted by
let orderby = parms.orderby != nil ? "&orderby=\(parms.orderby!)" : ""
// parts.limit is Int?
let limit = parms.limit != nil ? "&limit=\(parms.limit!)" : ""

var strURL: String
strURL = "\(baseURL)\(format)\(startTime)\(endTime)\(orderby)\(limit)"
</code>```

'''

I'm getting the result I need. E.g., if the user does not provide a StartDate parameter, it is not referenced in the query string.

'''
https://.....?format=geojson&endtime=2019-05-31&orderby=magnitude&minmagnitude=5&limit=2000
'''

but I have this feeling of missing the point of Swift Optionals

答:
Don't build a URL by concatenating strings. Use URLComponents, and an array of URLQueryItems.

You can then either:

not add items to the array of query items if the value is nil

create them with nil value strings and filter when you're done.

do something with flatMap for each optional parameter:

```<code>parms.startDate.flatMap {
   parameters.append(URLQueryItem(name: "startttime", value: "\($0)"))
}
</code>```

Body:
I'm building a RESTful API query using values collected from the user. Some of those values might not be supplied, so I'm using optionals.  There are Double, Int and String values.  The code I'm using seems clunky.  Is there a cleaner, Swifty way to write this block of code?

'''Swift

let baseURL = "http......"

// Start with required parms
let format = "?format=\(parms.format)"
let endTime = "&endtime=\(parms.endDate)"

// now for optionals
let startTime = parms.startDate != nil ? "&starttime=\(parms.startDate!)" : ""

// Sorted by
let orderby = parms.orderby != nil ? "&orderby=\(parms.orderby!)" : ""
// parts.limit is Int?
let limit = parms.limit != nil ? "&limit=\(parms.limit!)" : ""

var strURL: String
strURL = "\(baseURL)\(format)\(startTime)\(endTime)\(orderby)\(limit)"

'''

I'm getting the result I need. E.g., if the user does not provide a StartDate parameter, it is not referenced in the query string.

'''
https://.....?format=geojson&endtime=2019-05-31&orderby=magnitude&minmagnitude=5&limit=2000
'''

but I have this feeling of missing the point of Swift Optionals

Body:
Don't build a URL by concatenating strings. Use URLComponents, and an array of URLQueryItems.

You can then either:

not add items to the array of query items if the value is nil

create them with nil value strings and filter when you're done.

do something with flatMap for each optional parameter:

parms.startDate.flatMap {
   parameters.append(URLQueryItem(name: "startttime", value: "\($0)"))
}